Results 1 to 15 of 15

Thread: Raising errors in .NET

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Raising errors in .NET

    VB6:
    VB Code:
    1. Dim objConn As Connection
    2. On Error Goto ErrHandler:
    3.  
    4.    'code
    5.  
    6.    Exit Sub
    7. ErrHandler:
    8.    If Not (objConn Is Nothing) Then
    9.       objConn.Close
    10.       Set objConn = Nothing
    11.    End If
    12.    Err.Raise Err.Number, Err.Source, Err.Description

    VB.NET
    VB Code:
    1. Dim Conn As SQLConnection
    2.    Try
    3.  
    4.       'code
    5.  
    6.    Catch Ex As Exception
    7.       If Not (Conn Is Nothing) Then
    8.          Conn.Close
    9.          Conn = Nothing
    10.       End If
    11.  
    12.       'Want to raise exception to UI here
    13.  
    14.    End Try
    How do I do this?

    Woka

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Raising errors in .NET

    Throw New Exception("DFKJ")

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Raising errors in .NET

    Quote Originally Posted by mendhak
    Throw New Exception("DFKJ")
    Naughty froggy.

    Throw New ApplicationException("DFKJ")
    I don't live here any more.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Raising errors in .NET

    woka... there is a try/catch example app in the 101 VB.NET examples

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Raising errors in .NET

    also... unless your design model calls for a need to do it like that.. i would move any object disposing code and DB closing code to the finally clause.. so it is run if you error or not...

    sort of like this
    VB Code:
    1. Public Sub DoFunThings()
    2.         Dim CN As SqlConnection
    3.         Dim CMD As SqlCommand
    4.         Try
    5.             CN = New SqlConnection(DBConnString)
    6.             CN.Open()
    7.             CMD = New SqlCommand("SELECT MARCLAR FROM MARCLAR WHERE [MARCLAR] = MARCLAR", CN)
    8.             'PUT SOME CODE HERE THAT DO SOMETHING FUN
    9.         Catch ex As SqlException
    10.             'ERROR HANDLER HERE FOR DB ERROR
    11.         Catch ex As Exception
    12.             'ERROR HANDLER HERE FOR ANY OTHER
    13.         Finally
    14.             Try
    15.                 If Not CN Is Nothing Then
    16.                     If CN.State <> ConnectionState.Closed Then
    17.                         CN.Close()
    18.                     End If
    19.                     CN.Dispose()
    20.                     CN = Nothing
    21.                 End If
    22.                 If Not CMD Is Nothing Then
    23.                     CMD.Dispose()
    24.                     CMD = Nothing
    25.                 End If
    26.             Catch ex As Exception
    27.             End Try
    28.         End Try
    29.     End Sub

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Raising errors in .NET

    Also, as you may have noticed, raising exceptions is more costly than vb6 error handling was. There is a noticeable pause prior to the catch block being caught. This makes raising your own errors a less appealing prospect.

    When an exception is raised in .NET, I actually get to think "Gee, I wonder what it will be this time?" as I wait for the actual message to come up. I don't remember that long pause using On Error in vb6.
    My usual boring signature: Nothing

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Raising errors in .NET

    I have only noticed that when in debug mode... or possibly on the first error raised in a release mode exe... other than that i see no lag in the error handling

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Raising errors in .NET

    Quote Originally Posted by kleinma
    I have only noticed that when in debug mode... or possibly on the first error raised in a release mode exe... other than that i see no lag in the error handling
    I was wondering about that as I typed it. I, also, have only noticed that in debug mode, but I can't decide if that was because the lag was absent, reduced, or just because I was more attuned to it when in debug mode.
    My usual boring signature: Nothing

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Raising errors in .NET

    Quote Originally Posted by wossname
    Naughty froggy.

    Throw New ApplicationException("DFKJ")
    It would depend on the severity of the exception Woka needs to throw.
    If its a non-fatal error then mendhaks corrected example by wossname will be ok.
    If its fatal then you need to use -

    VB Code:
    1. Throw New System.Exception("Connection Error!")' Or whatever you want to display.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Raising errors in .NET

    from MSDN
    ApplicationException does not provide information as to the cause of the exception. In most scenarios, instances of this class should not be thrown. In cases where this class is instantiated, a human-readable message describing the error should be passed to the constructor.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Raising errors in .NET

    Ah, that makes it even worse for the frog Time to return your MCAD Jk.

    You can add the source and description properties of the exception for a useful error message.

    VB Code:
    1. Throw New System.Exception(ex.Source & " - " & ex.Message)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Raising errors in .NET

    Quote Originally Posted by RobDog888
    Ah, that makes it even worse for the frog Time to return your MCAD Jk.

    You can add the source and description properties of the exception for a useful error message.

    VB Code:
    1. Throw New System.Exception(ex.Source & " - " & ex.Message)
    rob.. wouldn't that mean you would already have to have an exception if you are using the ex.source and ex.message?

    Like
    VB Code:
    1. Try
    2.     'something
    3. Catch ex as system.exception
    4.     Throw New System.Exception(ex.Source & " - " & ex.Message)
    5. end try

    i would think this would be just as good if your goal is to pass the exception up a level
    VB Code:
    1. Try
    2.     'something
    3. Catch ex as system.exception
    4.     Throw ex
    5. end try

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Raising errors in .NET

    I didnt realize that you could short hand it that way, but yes I was referring to your first example.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Addicted Member WALDO's Avatar
    Join Date
    Aug 2002
    Location
    Swing of Prussia, PA
    Posts
    244

    Red face Even shorter

    Quote Originally Posted by RobDog888
    I didnt realize that you could short hand it that way
    VB Code:
    1. Try
    2.     'something
    3. Catch ex As System.Exception
    4.     Throw
    5. End Try

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Raising errors in .NET

    ApplicationWHAT?

    It's no good because *I* say so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width