Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Err.Raise Runtime Error - Revisited

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
Since I didn’t get any responses to my previous post (2/15/02), I assume the way I worded the question was far too complicated. Included below is code that will duplicate my problem. (You should be able to copy/paste to check it out)

Problem: If I call Main (via the debugger or whatever), Main02 reports the error and sends it up the call stack. Neither Main01 nor Main report the error again.

However, if I call Main01 (via the debugger), Main02 reports the error and Main01 gives me a runtime error on the Err.Raise statement (because Main01 is at the top of the calling stack).

Is there a way to prevent Main01 from displaying the runtime error when it is at the top of the calling stack?

Obviously, I could check "mlngErrNumber" after each call, but because of complexity and retrofitting, this isn't practical.

Option Compare Database
Option Explicit

Dim mlngErrNumber As Long
Dim mstrErrDescription As String

Function Main()

On Error GoTo ErrHandler

'blah blah blah

Call Main01 'If error raised, goto ErrHandler

'blah blah blah

ExitProcedure:
Exit Function

ErrHandler:

mlngErrNumber = Err.Number
mstrErrDescription = Err.Description

If (Err.Number > 0) Then 'If True, Error not reported yet
MyMsgBox
End If

Resume ExitProcedure

End Function

Function Main01()

On Error GoTo ErrHandler

'blah blah blah

Call Main02

'blah blah blah

ExitProcedure:
Exit Function

ErrHandler:

mlngErrNumber = Err.Number
mstrErrDescription = Err.Description

If (Err.Number > 0) Then 'If True, Error not reported yet
MyMsgBox
mlngErrNumber = mlngErrNumber + vbObjectError
End If

Err.Raise mlngErrNumber, "Main01," & Err.Source, mstrErrDescription

End Function

Function Main02()

Dim i As Integer

On Error GoTo ErrHandler

'blah blah blah

i = 6 / 0

'blah blah blah

ExitProcedure:
Exit Function

ErrHandler:

mlngErrNumber = Err.Number
mstrErrDescription = Err.Description

If (Err.Number > 0) Then 'If True, Error not reported yet
MyMsgBox
mlngErrNumber = mlngErrNumber + vbObjectError
End If

Err.Raise mlngErrNumber, "Main02," & Err.Source, mstrErrDescription

End Function

Public Function MyMsgBox()

On Error GoTo ErrHandler

'blah blah blah

MsgBox mstrErrDescription

'blah blah blah

ExitProcedure:
Exit Function

ErrHandler:
Resume ExitProcedure

End Function

 
In function2 u have an i as integer " i = 6/0"

Is it possible to divide by 0?
 
No it's not possible to divide by 0. I put that in there to force an error. (By the way, I'm using Access 2000)

If you copy the code and insert it into a new module and then type: Main in the immediate debug window you will see it work (Main is at the top of the call stack). Next, if you type Main01 in the immediate debug window you will see the untrappable error occur (Main01 is now at the top of the call stack).

I want Main02 to display the error and then raise the error again so that Main01 will respond to the error. However, since Main02 already displayed the error, Main01 will not display it again, it will simply raise the error again so that Main will respond to it. Again, since Main02 displayed the error Main will not display it. This works like I want.

However, if Main01 is at the top of the call stack (rather than Main) I get an untrappable runtime error when Main01 attempts to raise the error.

I really appreciate you taking the time to look at the problem. Since I haven't received any responses I wasn't sure if what I want to do is possible or not. Or if my explanation of the problem is too complicated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top