Results 1 to 3 of 3

Thread: error handling strategy

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    error handling strategy

    We are in the process of writing a fairly big application and I am trying to design an error handling strategy.

    Although the app is currently being written in VB6 we will be porting it over to .net in about a year or so. So we want to limit the work involved when we do.

    I am interested in 2 types of errors. Those I plan for in the code (custom number 1002) e.g. missing date of birth, and those I have not planned for (custom number 1001) e.g. missing file, invalid use of null.

    What we want to do is log an error in the database in the function/sub it occurs and then raise a custom error to the calling functions until the error is handled at an appropriate time, e.g. Button click event where an appropriate message is displayed to the user.

    As there are any number of functions between the button click event and the function the error occured in some functions will only simply re-raise the error for its calling function to handle.

    For this to work I need to know which function the error originated so the error is not logged twice and/or whether the error has been logged.

    I have some code to demonstrate this.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        On Error Goto Abort
        
        func1
        
        Exit Sub
        
    Abort:
        logerror err
        MsgBox err.Description, vbCritical, "An error occurred"
    End Sub
    
    Private Sub func1()
        On Error Goto Abort
            
        func2
        
        Exit Sub
    
    Abort:
        If err.Number <> 1001 Then
            logerror err
        End If
        err.Raise err.Number, err.Description
    End Sub
    
    Private Sub func2()
        On Error Goto Abort
            
        func3
        
        Exit Sub
    
    Abort:
        If err.Number <> 1001 Then
            logerror err
        End If
        err.Raise err.Number, err.Description
    
    End Sub
    Private Sub func3()
    
        On Error Goto Abort
        
            err.Raise 1002, , "Missing Date of Birth"
            
        Exit Sub
    
    Abort:
        logerror err
        
        If err.Number <> 1002 Then
            err.Raise 1001, "Unexpected Application Error"
        End If
    End Sub
    
    Private Sub logerror(errError As ErrObject)
    
        MsgBox errError.Number & "  " & errError.Description & vbCrLf & vbCrLf & "The error has been logged"
    
    End Sub
    In .net there are exception rather than errors. I really need an exception handling class to achieve this.


    Any thoughts or comments on how to do this in vb.net would be appreciated.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    I'd just use some try and catch blocks so it will catch the error and you can handle it appropriatly.

    Also, using the GoTo statement is usually a bad coding habit, you should try and avoid it when possible

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    The code example above is from the VB6 version, I am still trying to get to grips with the try and catch method of handling exceptions.

    I guess the method behind the madness is the same.

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