Error Handler in a Public Sub (1 Viewer)

freuzo

Member
Local time
Today, 07:48
Joined
Apr 14, 2020
Messages
98
Hi everyone,

I put one of my codes in Bing Chat and asked him to help me refactor it. It did, but in the end, instead of throwing my Error Handler as it was, it did it like this:

Code:
Form_Load_Error:
    ShowErrorMessage "Form_Load", Erl
End Sub


Public Sub ShowErrorMessage(ByVal strProcedureName As String, ByVal intLineNumber As Integer)
   MsgBox "Errorr " & Err.Number & " (" & Err.Description & ") in procedure " & strProcedureName & ", line " & intLineNumber & "." & vbCr & "Contact admin.", vbInformation, "ZM"
End Sub

What I had before:
Code:
Form_Load_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_Load, line " & Erl & "." & vbCr & "Contact admin.", vbInformation, "ZM"
End Sub

Isn't this a cleaner way to handle errors?
 

Ranman256

Well-known member
Local time
Today, 02:48
Joined
Apr 9, 2015
Messages
4,337
Code:
sub mySub()
On error goto ErrBlock

do stuff here

exit sub
ErrBlock:
msgbox err.description,,err
end sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 01:48
Joined
Feb 28, 2001
Messages
27,186
Isn't this a cleaner way to handle errors?
One should never forget that there are TWO parts to error handling.

One part is reporting the error, either/both to the user and/or to a log file periodically reviewed by the application admin.

The other part is "what do you DO about the error (programmatically)?" I.e. the error handler needs to address the problem that was discovered.

What you are doing is quite reasonable and not that different from an approach I used several years ago as the "reporting" part of the trap handler. But there was unique, local code to go with each trap to decide how to recover (or IF recovery was even possible) from the error in question. During app trials and testing, I uncovered the most common errors and devised ways of responding. The biggest problem was to recognize and manage a way to say, "Oh, blast it all, that error is fatal" and clean up the mess without storing bad data.
 

Users who are viewing this thread

Top Bottom