Subject: Bubble up errors - noteslimit
I’m sure there are several ways to overcome any limitations but here’s how I handle errors…
I normally log each error at each level (to an error log [rich-text field upon an execution log document]) and then re-throw the error to ensure that the whole call stack is included in the error text. So the error log would look something like this:
Error at line 9 in NestedFunctionC - ‘Object variable not set’
Error at line 59 in NestedFunctionB - ‘Object variable not set’
Error at line 24 in NestedFunctionA - ‘Object variable not set’
Error at line 14 in Initialize - ‘Object variable not set’
To do this, when an error occurs it is logged (at the level at which it occurs) and is then re-thrown to the error handler in the calling method. The code I use to re-throw the error sits in the error handler block (rather than in the method that writes the error to the rich-text field) and the code to log the error stays in a separate function/method. It’s not much different to how you’re already doing this, except there’s no need to build up a string of the calling stack since each re-throwing of the error logs the information at its current level (including the method name, the error line, and the error message) to the rich-text item. To rethrow the error at each level I clear the error handler (“on error goto 0”) and then rethrow the error (“error err, error$”).
I find it’s easiest to implement all this in OO code rather than procedural code, since OO allows a base class to be defined which contains the HandleError method, along with member variables used for the execution log and its rich-text body field. Then all sub-classes can just include the same error handler in each method which calls the base class handleerror method when anything goes wrong.
I hope this is of some help.
Procedural example:
Sub Initialize
On Error Goto ErrorHandler
'do something
Call NestedFunctionA
ExitPoint:
Exit Sub
ErrorHandler:
HandleError Err, Error$, Erl
'don’t re-throw in module Initialize as there’s no point
Resume ExitPoint
End Sub
Sub NestedFunctionA
On Error Goto ErrorHandler
Call NestedFunctionB
ExitPoint:
Exit Sub
ErrorHandler:
HandleError Err, Error$, Erl
On Error Goto 0 'invoke the calling module’s error handler
Error Err, Error$ 'rethrow the error
End Sub
Sub NestedFunctionB
On Error Goto ErrorHandler
Call NestedFunctionC
ExitPoint:
Exit Sub
ErrorHandler:
HandleError Err, Error$, Erl
On Error Goto 0 'invoke the calling module’s error handler
Error Err, Error$ 'rethrow the error
End Sub
Sub HandleError(PlngError As Long, PstrError As String, PlngLine As Long)
'append the error to a richtext item upon an execution log document (which is kept either as a global variable or a member variable if the methods are part of a class)
'use Getthreadinfo(10) (as you already do) to fetch the name of the module in which the error is being handled
End Sub