dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] VB Errors - Exception Names

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] VB Errors - Exception Names

    hello
    i was wondering if there was some way you can loop the exception names in vb and return the name of the exception that you got?

    Code:
    Public Sub MYSub()
    Try
    'Code
    Catch ex As Exception
    Msgbox(GetExceptionName(e))
    End Try
    End Sub
    Code:
    Public Function GetExceptionName(ByRef e As Exception) As String
    Dim Err As String
    Return  "Exception Type:" & Err
    End Function
    Last edited by sinner0636; Jan 24th, 2018 at 05:59 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,504

    Re: VB Errors - Exception Names

    I've never thought about this, but I believe that all exceptions are just types derived from a base Exception type. Therefore, you might see whether the type name of the exception object was what you were looking for. Normally, you can do this by calling .ToString on the object, but exceptions override the .ToString implementation to give you exception information, so that won't work. Still, you might try playing with GetType and see whether that can get you what you want.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: VB Errors - Exception Names

    Your right think i might have got it more simple then what i thought i may be wrong though its giving the exception names the right way

    Code:
    Public Sub SomeSub
    Try
    'Code
    Catch ex As Exception
    Msgbox(GetExceptionName(ex))
    End Try
    End Sub
    Code:
      Public Function GetExceptionName(e As Exception) As String
            Try
                Dim Err As String = e.GetType().ToString
                Return Err
    
            Catch ex As Exception
                Return ex.Message 'Err got itself lol!
            End Try
        End Function
    Last edited by sinner0636; Jan 24th, 2018 at 05:21 PM.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: VB Errors - Exception Names

    I'm more interested in, "Why do you want to know?", because there's probably a better way to do what you want.

    I also think there's a little language ambiguity.

    If you just want to know the name of the exception you caught, you can ask it for its type, then use the type's name:
    Code:
    Catch ex As Exception
        MessageBox.Show(ex.GetType().Name)
    End Catch
    But if you're trying to do different things for different exceptions, it's better to use multiple catch blocks:
    Code:
    Try
        ' something
    Catch ex As InvalidOperationException
        ' handle this case
    Catch ex As FileNotFoundException
        ' handle this case
    Catch ex As Exception
        ' handle this case
    End Try
    The 'ex As Exception' version is very, very expansive, and not philosophically a good idea for many cases. We handle exceptions because we understand the situation that leads to them being thrown and think we can do something to recover from it. The only good use for "handle everything" is to log it before, presumably, giving up entirely and potentially shutting down the entire program. The right thing to do is very case-by-case.

    In terms of, "Can I get a list of the name of every possible exception that could be thrown?", the answer is only yes for a subset of all possible exceptions. Technically, any assembly can make a new exception, so "any valid identifier name" could be an exception type. The subset of the question you can answer is, "Can I get a list of the Exception types that Microsoft has created?" You can do that. It takes some reflection code, but it's not super difficult. Still, I have a feeling it's not really going to be the best way to get to what you want.

    This is the "how" you want to go about answering some other question. I'm more curious about the question, because I wager there's a better "how".
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: [RESOLVED] VB Errors - Exception Names

    yeah agree but that is a lot of coding come up with something better to error handle exceptions in a quick dirty way

    Code:
    Try
    'Run Code
    Catch ex As Exception
    Call GetFileExceptionNames(ex.GetType().Name, ex.Message)
    End Try
    Code:
    Public Sub GetFileExceptionNames(ExceptionName As String , ExceptionMsg As String ) 
            Select Case ExceptionName
                Case "IOException"
                  'Do  Something
                Case "UnauthorizedAccessException"
                  'Do  Something
                Case "FileNotFoundException"
                  'Do Something Else
                Case "AccessViolationException"
                  'Do Something Else
                Case Else
                   MsgBox(ExceptionName & ExceptionMsg ) 'File Exception Not Listed 
           End Select
    
    End Sub
    Last edited by sinner0636; Jan 28th, 2018 at 04:40 PM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width