Error handling - pass error to caller? (1 Viewer)

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 02:21
Joined
Feb 28, 2001
Messages
27,001
In my current project, I have some situations where I want to pass an error back to the program that activates my routines. I know how to raise an error but the error causes a trap in the Class Module when I want the error to show up in the caller / user of the class module. I have On Error traps in the caller, so some of the online advice I saw doesn't seem to apply. The articles I found say the error will be passed up the call-frame stack until a declared handler is currently inactive but eligible to take the error at which time it will trap there.

The articles further suggest that I can get the desired effect by placing an error trap in the Class Module and have that handler re-raise the error before doing a Resume Next. But what actually happens is that it traps in the Class Object code.

Is there a trick to sending that error back up the line and NOT taking it in the Class Object?

Here is a sample:

Code:
Public Property Let SourceLine(TheLine As String)
'
'   prepare for parsing by loading up a string.  string can contain up to several thousand characters.
'   once all is reset, we are ready to actually load things up
'   measure line size, reset error code.  Token class-code is reset by Class_Reset call.
'
    On Error GoTo ErrHand
    If IsNull(TheLine) Then
        cbERR = True
        ceErrCode = PRSERR_INVAL_USE_OF_NULL
        Err.Raise ceErrCode, TypeName(Me), XLateErrCode(ceErrCode)
    Else
        Call Class_Reset                'erase all remnants.  resets almost everything.
        csClause = TheLine              'load up the string
        clCLength = Len(csClause)       'set the internal line length
        cbEOL = False                   'new line so not at EOL yet
        cbERR = False                   'and give benefit of the doubt at first
        ccTCCode = tclNoCls             'no parse has yet occurred so mark consistent with that fact
        ceErrCode = PRSERR_NO_ERROR     'no errors at the moment
        csPosPnt = ""                   'no error pointer
    End If

    Exit Property

ErrHand:

    Err.Raise Err.Number, TypeName(Me), XLateErrCode(Err.Number)
    
    Resume Next

End Property

The behavior is that when the error is triggered, the trap occurs in the ErrHand routine when I want it occur in the thing that called this Property Let routine.
 

vba_php

Forum Troll
Local time
Today, 02:21
Joined
Oct 6, 2019
Messages
2,884
per your statement, richard:
The articles further suggest that I can get the desired effect by placing an error trap in the Class Module and have that handler re-raise the error before doing a Resume Next. But what actually happens is that it traps in the Class Object code.
have you read the section called "Handling errors in nested procedures" on this page? https://docs.microsoft.com/en-us/of...ror-codes/elements-of-run-time-error-handling. per the page:
When an error occurs in a nested procedure that doesn't have an enabled error handler, Visual Basic searches backward through the calls list for an enabled error handler in another procedure, rather than simply halting execution.
I wonder if that applies to UDTs as well?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 03:21
Joined
May 21, 2018
Messages
8,463
Where did you tell it to break?
Break.jpg
 

Users who are viewing this thread

Top Bottom