• Error handling problem from evaling code that raises error

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Error handling problem from evaling code that raises error

    Author
    Topic
    #475642

    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

    Viewing 1 reply thread
    Author
    Replies
    • #1273094

      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

    • #1273169

      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

    Viewing 1 reply thread
    Reply To: Error handling problem from evaling code that raises error

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: