T
Trevor
I am experiencing an issue where a run-time exception does not make
its way back to the top-most subroutine. The run-time exception is
passed to the default Visual Basic error handler rather than being
handled by the exception handler of `Foo3' (see below).
Why does the error raised in the UserForm object not propagate back to
the subroutine in the public module (i.e. the MsgBox prompt is never
executed)?
The following subroutine is intended to create a call stack:
Public Sub RaiseError( _
ByRef r_lSubID As Long, _
ByRef r_lErrNum As Long, _
ByRef r_sErrDescrp As String, _
Optional ByRef r_sSubDescrp As String = Empty _
)
Call Err.Raise( _
Number:=vbObjectError + r_lSubID, _
Description:=r_sSubDescrp & vbCrLf & Format( _
expression:=IIf(r_lErrNum < 0, _
r_lErrNum - vbObjectError, r_lErrNum), _
Format:=g_sERR_NUM_FMT) & vbTab & r_sErrDescrp)
GoTo Finally
Finally:
Exit Sub
End Sub
The following subroutine (found in a class module named `clsFoo')
generates an error:
Public Sub Class_Initialize()
Dim foo As Variant
On Error GoTo ErrorHandler
'...
let foo = 1/0
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=1, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub
The following subroutine (found in a UserForm object named `frmFoo')
creates an instance of `clsFoo':
Public Sub Foo_Click()
Dim foo As clsFoo
On Error GoTo ErrorHandler
'...
Set foo = new clsFoo
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=2, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub
The following subroutine (found in a public module named `modFoo')
creates an instance of the `frmFoo' UserForm object:
Public Sub Foo3()
Dim foo as UserForm1
On Error GoTo ErrorHandler
'...
Set foo = new UserForm1
Call foo.Show()
'...
Goto Finally
ErrorHandler:
Resume TraceBack
TraceBack:
On Error GoTo 0
Call MsgBox(prompt:=Err.Description, Buttons:=vbCritical)
Finally:
Exit Sub
End Sub
its way back to the top-most subroutine. The run-time exception is
passed to the default Visual Basic error handler rather than being
handled by the exception handler of `Foo3' (see below).
Why does the error raised in the UserForm object not propagate back to
the subroutine in the public module (i.e. the MsgBox prompt is never
executed)?
The following subroutine is intended to create a call stack:
Public Sub RaiseError( _
ByRef r_lSubID As Long, _
ByRef r_lErrNum As Long, _
ByRef r_sErrDescrp As String, _
Optional ByRef r_sSubDescrp As String = Empty _
)
Call Err.Raise( _
Number:=vbObjectError + r_lSubID, _
Description:=r_sSubDescrp & vbCrLf & Format( _
expression:=IIf(r_lErrNum < 0, _
r_lErrNum - vbObjectError, r_lErrNum), _
Format:=g_sERR_NUM_FMT) & vbTab & r_sErrDescrp)
GoTo Finally
Finally:
Exit Sub
End Sub
The following subroutine (found in a class module named `clsFoo')
generates an error:
Public Sub Class_Initialize()
Dim foo As Variant
On Error GoTo ErrorHandler
'...
let foo = 1/0
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=1, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub
The following subroutine (found in a UserForm object named `frmFoo')
creates an instance of `clsFoo':
Public Sub Foo_Click()
Dim foo As clsFoo
On Error GoTo ErrorHandler
'...
Set foo = new clsFoo
'...
Goto Finally
ErrorHandler:
Dim lErrNum As Long
Dim sErrDescrp As String
Let lErrNum = Err.Number
Let sErrDescrp = Err.Description
Resume TraceBack
TraceBack:
On Error GoTo 0
Call RaiseError(r_lSubID:=2, r_lErrNum:=lErrNum,
r_sErrDescrp:=sErrDescrp)
Finally:
Exit Sub
End Sub
The following subroutine (found in a public module named `modFoo')
creates an instance of the `frmFoo' UserForm object:
Public Sub Foo3()
Dim foo as UserForm1
On Error GoTo ErrorHandler
'...
Set foo = new UserForm1
Call foo.Show()
'...
Goto Finally
ErrorHandler:
Resume TraceBack
TraceBack:
On Error GoTo 0
Call MsgBox(prompt:=Err.Description, Buttons:=vbCritical)
Finally:
Exit Sub
End Sub