Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Error handling problem from evaling code that raises error

    Hi all
    (Access 2002 VBA)
    I've got a need to catch errors that are raised from some code that is evaluated with eval. Here's some sample code that displays my problem.

    Code:
     
    Function testFn()
        Err.Raise 100
        testFn = 5    'Correctly, this doesn't get evaluated
    End Function
    Sub testEval()
        On Error GoTo ErrorHandler
        Dim result As Integer
        'Err.Raise 100    'If this was here, works as expected
        result = Eval("testFn()")   'This should raise error, but it is unhandled
        Debug.Print "Result is", result
        Exit Sub
        
    ErrorHandler:
        'Should end up here, but doesn't
        Debug.Print "Error", Err.Number, "handled"
    End Sub
    The problem is that the error raised should be caught by the ErrorHandler, but it isn't. Instead, VBA displays the familiar 'run time error'.
    It doesn't matter what my Tools>Options>General>Error trapping setting is.
    Should I be able to catch a raised error from eval'd code? If not, what are my other options?
    Naturally, MS documentation is of zero help.
    Thanks for any ideas.
    Peter

  2. #2
    2 Star Lounger
    Join Date
    Mar 2010
    Location
    Tampa, FL, USA
    Posts
    114
    Thanks
    11
    Thanked 10 Times in 9 Posts
    The error handler has to be able to traverse the call stack to locate the last active error handler when there isn't one in the local routine. The Eval() function doesn't manage the call stack the same as Call. This is evident if you press ctrl-L when in the function called via Eval() to see an example.

    The code below works correctly as it doesn't need to reverse walk the call stack to locate the error handler.

    I'd suggest two options:
    1. Don't use Eval()
    2. In Eval()'d routines, create local error handlers.

    Code:
    Sub testEval()
        On Error GoTo ErrorHandler
        Dim result As Integer
        Err.Raise 100    'Works as expected
        result = testFn(101)   'This should raise error and get handled
        result = Eval("testFn(102)")   'This should raise error, but it is unhandled unless local error handler active
        Debug.Print "Result is", result
        Exit Sub
        
    ErrorHandler:
        'Should end up here, but doesn't
        Debug.Print "Error", Err.Number, "handled"
        Resume Next
    End Sub
    
    
    
    Function testFn(ByRef errnum As Long)
        On Error GoTo LocalErrorHandler   ' Remove this line to generate run-time error
        Debug.Print "Call stack is? (Press ctrl-L)"
        Err.Raise errnum
        testFn = 5    'Correctly, this doesn't get evaluated
        Exit Function
        
    LocalErrorHandler:
        Debug.Print "Error", Err.Number, "handled locally in called function"
        Resume Next
    End Function
    =====================
    PJ in FL
    PJ in FL

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Sydney, Australia, New South Wales, Australia
    Posts
    251
    Thanks
    0
    Thanked 4 Times in 4 Posts
    Thanks PJ. I was hoping it would manage the call stack, but as you have clearly shown it doesn't.
    Yes, I'll need to manage it locally.
    Thanks for the clear explanation.
    Peter

Posting Permissions

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