Results 1 to 15 of 15

Thread: Error Handling class?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18

    Error Handling class?

    Hi there,
    I am trying to write a simple error handling class and I can't seem to find any kind of samples on the web. I am catching the error like so...(errorhandling being my errorhandling class object)
    Code:
        'class variables
        Dim errorString As String
        Dim exh As errorhandling
    
    Public Sub....
    .....'code
    .......
            Catch ex As Exception
                trans.Rollback()
                exh = CType(context.Handler, errorhandling)
                'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
                errorString = exh.handle(ex)
                Response.Redirect("Error.aspx?errorMsg=" & errorString)
            Finally
                If myConn.State = ConnectionState.Open Then
                    myConn.Close()
                End If
                myConn = Nothing
            End Try
    When I run it as is gives me a casting error with my ctype conversion...?? I can't even get into my error handling class to check if it works! Please, what am I missing here?

    Another thing I'm trying to figure out is how I can access the specific error number? I know that Exceptions have a HResult() method but it's protected and I'm not sure how else I can access the values.

    Any help on these issues would be most appreciated.
    Cheers.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this
    VB Code:
    1. Dim errorString As String
    2. Dim exh As errorhandling
    3.  
    4. try
    5.  
    6.  
    7.         Catch ex As Exception
    8.             trans.Rollback()
    9.             exh = CType(context.Handler, errorhandling)
    10.             'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
    11.             errorString = exh.handle(ex)
    12.             Response.Redirect("Error.aspx?errorMsg=" & errorString)
    13. 'it should be here to catch the error if raised .
    14. [B]catch x as execption [/B]
    15.  
    16.             If myConn.State = ConnectionState.Open Then
    17.                 myConn.Close()
    18.             End If
    19.             myConn = Nothing
    20.  
    21.  
    22. messagebox.show (x.message)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    On inserting that, it gets stuck when I try to call the handle() method in my error handling class:
    (where errorString is a string looking for a return error string from the errorhandling class and exh is an instance of the errorhandling class)
    Code:
    errorString = exh.handle(ex)
    ...it gives me the error:

    "Object reference not set to an instance of an object"

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Try this now :
    VB Code:
    1. try
    2. 'Remove this and rebuild the try structure !
    3. Catch ex As Exception
    4. trans.Rollback()
    5. exh = CType(context.Handler, errorhandling)
    6. 'handle(ByVal ex as Exception) is the function which the ecception is passed to in the err handling class
    7. errorString = exh.handle(ex)
    8. Response.Redirect("Error.aspx?errorMsg=" & errorString)
    9. 'it should be here to catch the error if raised .
    10. catch x as execption  
    11.  
    12. Dim errorString As String
    13. Dim exh As errorhandling
    14. If myConn.State = ConnectionState.Open Then
    15. myConn.Close()
    16. End If
    17. myConn = Nothing
    18.  
    19. messagebox.show (x.message)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    I'm so dozy I just forgot to instantiate the errorhandling class object. I have since done this and can now access the class...but I don't suppose you could point me in the right direction as to how I can access the error codes (I want to use a Select statement and firstly look for which exception occured and secondly, the different types of SQLExceptions etc.) and send back a manipulated string according to which kind of error it is?
    Much thanks for your help.

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Oops .

    For detailed info about generated errors , you can access different functions of the System.Exception class . Like :
    Dim Exception as Exception

    Exception.Stack
    Exception.Source
    Exception.Site
    Exception.Message

    then you can combine them all togother and show up a messagebox or write them to log file . Is this what you're asking about ?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    Not quite. I was under the impression that it was possible to get the associated error number of an error (just as a simple example...
    [code]

    Public Function handle(ByVal ex As Exception)
    Dim exNo As Integer

    Select Case exNo
    Case 111
    if exno = 1
    Return "An SQL Exception has occured. Type:"
    Case 222
    Return "An IO Exceptoion has occured. Type:"
    Case Else
    s = ex.ToString
    Return s
    End Select

    End Function

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    As far as I know , there is no associated number with exception error in .NET .

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    Not quite. I was under the impression that it was possible to get the associated error number of an error.
    An example of the idea I'm trying to work out...
    Code:
        Public Function handle(ByVal ex As Exception)
            Dim exNo As Integer
            Dim s As String
            exNo = ex.HResult()
            s = ex.ToString
    
            Select Case exNo
                Case 111
                    If exNo = 1 Then
                        Return "An SQL Null Value Exception has occured. Type:" & s
                    ElseIf exNo = 2 Then
                        Return "An SQL Exception has occured. Type:" & s
                    End If
    
                Case 222
                    Return "An IO Exception has occured. Type:" & s
    
                Case Else
                    s = ex.ToString
                    Return s
            End Select
    
        End Function
    But HResult is a protected method of the Exception class...is there not some way I can access this to find out the specific error numbers?

  10. #10
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Since you are working on handling specific exception "SQL Exception" , why don't you use it instead of the generic exception class ? Like this :

    This gets the error number I guess .

    VB Code:
    1. Catch ex As SqlClient.SqlException
    2. MessageBox.Show(ex.Number())

    and so on .

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    because I don't know what type of error has occured until it reaches my errorhandling class!

    Basically I want to catch whatever error occurs, pass it to a my errorhandling class, figure out what type it is and return an appropriate string back to where the error occured.

    How can I figure out what type of error has occured?

  12. #12
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    SQLExceptions have associated error numbers. For example, error number 229 is a permissions error. You can access it by myException.Number where myException is a SQLException. Regular Exception types don't have a number associated.

    I also trap specific SQL Exception Numbers in some of my code to take the appropriate action. Works well for me!

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by kto
    How can I figure out what type of error has occured?
    Call this little sub to see what type of exception it's going to throw . This is one way .
    VB Code:
    1. Private Sub callme()
    2.         Dim errortype As String
    3.         Dim ex As Exception
    4.  
    5.         Try
    6.             Dim a As Integer = "aaaa"
    7.  
    8.         Catch ex
    9.             'here you can figure out what error was generated
    10.             'simply by converting exception type to
    11.             'string
    12.             errortype = ex.GetType().ToString
    13.             MessageBox.Show(errortype.ToString)
    14.  
    15.         End Try
    16.  
    17.     End Sub

  14. #14
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is the other way . It uses GetType
    VB Code:
    1. Private Sub callme()
    2.         Dim errortype As String
    3.         Dim ex As Exception
    4.  
    5.         Try
    6.             Dim a As Integer = "aaaa"
    7.  
    8.         Catch ex
    9.  
    10.         End Try
    11.  
    12.  
    13.         'here you have to list exceptions that are expect to be thrown
    14.         'in this way .
    15.         If ex.GetType Is GetType(InvalidCastException) Then
    16.  
    17.             MsgBox("Error Type " & ex.Message & "Do whatever here ")
    18.  
    19.         ElseIf ex.GetType Is GetType(ExecutionEngineException) Then
    20.             'do your code here
    21.  
    22.         ElseIf ex.GetType Is GetType(InvalidProgramException) Then
    23.             'do your code here
    24.  
    25.         End If
    26.     End Sub

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2003
    Location
    Eire
    Posts
    18
    cheers mate, you've made this wee Irish lass very happy!

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