I have the following method that all the error handlers call:
Public Function ToError(strClass As String, strMethod As String) As String
On Error GoTo errHandle
ToError = "Err " & Err.Number & _
", Src: " & Err.Source & _
", Dsc: " & Err.Description & _
", Project: " & App.Title & _
", Class: " & strClass & _
", Method: " & strMethod & _
", Line: " & Erl
Err.Clear
exitPoint:
Exit Function
errHandle:
oLog.AddToLog "Error in ToError Method: " & Err.Description, False
Resume exitPoint
End Function
It turns out that because I declare an error handler in this function On Error GoTo errHandle
, VB6 clears the error before I am able to record it.
Is there a way to prevent the 'On Error GoTo errHandle' statement from clearing the error?
An
On Error
statement will always clear theErr
variable (Erl
will also be reset to 0). In theory this means you could fix the problem by moving theOn Error
statement below theToString = ...
line (or removing the error handler in theToError
function altogether), but unfortunately that won't necessarily always work either.Each component (DLL, ActiveX EXE, etc.) referenced by your project essentially gets its own
Err
instance in memory. So, if yourMainApp.exe
raises an error which gets passed toToError
(residing in a separateErrorHandling.dll
for example), the DLL won't see theErr
variable that your EXE sees. They each have their own privateErr
variables.There are a at least two ways around the problem that I can think of:
Method 1
As Zian Choy mentions, you could add additional parameters to your
ToError
function, one for each property of theErr
object that you need access to.Code
Example usage
You would then have to call like this from your error handlers like so, passing it all the relevant values from the current
Err
object, along withErl
:If you also want
App.Title
, you're going to have to add an additional parameter toToError
for that as well, sinceApp.Title
will be equal to theApp.Title
of the project where theToError
method is defined, not the component where the error was raised. This is important ifToError
is in a different project.Method 2
You can make your
ToError
calls a little less verbose by passing theErr
object itself as a parameter to the function, however the first thing yourToError
function should do in this case is immediately store a copy of all the relevant properties you need since a subsequentOn Error
statement will clear the variable.Code
Example Usage
You may be able to solve the problem by passing the values of the Err object into ToError as parameters.
You can create a User Defined Type as below
Later modify ToError function as below :
There's no way to prevent
On Error
clearing the error.ToError
. It's so short and bland it's unlikely to ever experience an error.ToError
code is inline in a general purpose error reporting routine, which performs logging or whatever is needed. Then use the techniques in Mike's answer.BTW If anyone reading this is adding their error handlers manually, stop whatever you are doing and immediately get the free MZ-Tools package.