Results 1 to 5 of 5

Thread: Global error handler with stack tracing

Threaded View

  1. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Global error handler with stack tracing

    From the linked article:
    Imagine you have opened a file and are attempting to roll back in your error handler. How do you know whether or not you opened the file? In other words, did the error occur before or after the line of code that opens the file? If you attempt to close the file and it's not open, you'll cause an error-but if it's open, you don't want to leave it open as you're trying to roll back! I guess you could use Erl to determine where your code erred, but this implies that you're editing line numbered source code-yuck. (You'll recall from Tip 2 that we added line numbers only to the code for the final EXE, not to the code we're still editing.) Probably the best way to determine what did or did not get done is to limit the possibilities; that is, keep your routines small (so that you have only a small problem domain). Of course, that's not going to help us here. What we need to do is apply a little investigation!
    I don't seem to have these issues with my implementation. This type of function would be what I call a non-root function that needs to close objects. So it would go like this:
    Code:
    Public Function ReadFile() As String
    On Error GoTo ReadFileErr
        Dim lngFileNumber As Long
    
        ErrorHandler eaEnter, ModuleConstant & ".ReadFile"
        ' Your code to access data/create objects goes here
        lngFileNumber = FreeFile()
        ' Open file and do stuff here
        ErrorHandler eaExit
    
    ReadFileExit:
        On Error Resume Next
        Close lngFileNumber
        On Error GoTo 0
        ErrorHandler eaRaise
        Exit Function
    
    ReadFileErr:
        ErrorHandler eaLog
        Resume ReadFileExit
    End Function
    Also from the linked article:
    If you've cleaned up locally all the way through the call chain and if you had an error handler for each stack frame (so that you didn't jump over some routines), you should now have effectively rolled back from the error.
    I see no point in trapping errors down the call stack that have no cleanup requirements. In other words, why would it be a problem to jump over routines that didn't need to close files or release objects? I don't get it.

    Every function has the Enter and Exit calls in order to track the call stack, but why would actual trapping be required?
    Last edited by Ellis Dee; Sep 21st, 2009 at 02:53 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
  •  



Click Here to Expand Forum to Full Width