Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centralized error handling in VB6

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?

like image 611
AngryHacker Avatar asked Mar 29 '10 23:03

AngryHacker


People also ask

What is centralization error handling?

Centralized way involves some sort of central database or storage of errors (usually a module or a header file) where are all the error messages stored. In code you pass an error code to some function and it does all the work for you. A big plus of this approach is that you have everything in one place.

How to handle errors in VB6?

You can end the program by coding an Exit Sub statement within the Error Handler or just let the program reach the End Sub and end that way. 2. You can instruct Visual Basic to continue running the program at the same line of code that caused the error to begin with. You do this by coding a Resume statement.

What is error handler in Visual Basic?

The Visual Basic error handling model allows programmers to perform special actions when an error occurs, such as jumping to a particular line of code. When an exception occurs in the Active Expert, the standard Visual Basic error handling works as expected.

What is the difference between error handling and debugging?

Instead, if an error does occur, your program can inform the user in a much more user-friendly manner, and you can still retain control over the program. Debugging , on the other hand, involves finding errors and removing them from your program.


3 Answers

An On Error statement will always clear the Err variable (Erl will also be reset to 0). In theory this means you could fix the problem by moving the On Error statement below the ToString = ... line (or removing the error handler in the ToError 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 your MainApp.exe raises an error which gets passed to ToError (residing in a separate ErrorHandling.dll for example), the DLL won't see the Err variable that your EXE sees. They each have their own private Err 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 the Err object that you need access to.

Code

Public Function ToError( _
   ByVal strErrSource As String, _
   ByVal nErrNumber As Long, _
   ByVal sErrDescription As String, _
   ByVal nLineNumber As Long) As String

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 with Erl:

ToError Err.Source, Err.Number, Err.Description, Erl 

If you also want App.Title, you're going to have to add an additional parameter to ToError for that as well, since App.Title will be equal to the App.Title of the project where the ToError method is defined, not the component where the error was raised. This is important if ToError is in a different project.

Method 2

You can make your ToError calls a little less verbose by passing the Err object itself as a parameter to the function, however the first thing your ToError function should do in this case is immediately store a copy of all the relevant properties you need since a subsequent On Error statement will clear the variable.

Code

Public Function ToError(ByVal oError As ErrObject, ByVal nLineNumber As Long) As String

   'Copy the important Err properties first, '
   'before doing anything else...            '

   Dim strErrSource As String
   Dim nErrNumber As Long
   Dim strErrDescription As String

   strErrSource = oError.Source
   nErrNumber = oError.Number
   strErrDescription = oError.Description

   On Error Goto errHandle

   'More code here
   '...

Example Usage

ToError Err, Erl
like image 156
Mike Spross Avatar answered Sep 28 '22 07:09

Mike Spross


You may be able to solve the problem by passing the values of the Err object into ToError as parameters.

like image 38
Zian Choy Avatar answered Sep 28 '22 07:09

Zian Choy


There's no way to prevent On Error clearing the error.

  • You could just remove the error handling from ToError. It's so short and bland it's unlikely to ever experience an error.
  • It might be better to refactor the error handling so that this 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.

like image 41
MarkJ Avatar answered Sep 28 '22 05:09

MarkJ