VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Add Error Handling

by Kamilche (35 Submissions)
Category: Coding Standards
Compatability: Visual Basic 3.0
Difficulty: Advanced
Date Added: Wed 3rd February 2021
Rating: (30 Votes)

This is the complete code for a VB IDE addin that will add error handling to your procedure. Requires that you have a routine called "HandleError" in a public module accessible to all routines. Sample HandleError routine follows:


Public Sub HandleError(ByVal CurrentModule As String, ByVal CurrentProcedure As String, _
ByVal ErrNum As Long, ByVal ErrDescription As String)
On Error GoTo Err_Init
MsgBox CurrentModule & " " & CurrentProcedure & ": " & ErrNum & " - " & ErrDescription
Exit Sub
Err_Init:
MsgBox CurrentModule & " HandleError: " & Err.Number & " - " & Err.Description
End Sub

The best VB code handles errors in every routine - this makes the program very robust. However, there's no easy way to determine WHICH routine failed once you're inside of your global error handler 'HandleError'. Therefore, you must pass the routine name to the global error. This can be very tedious! :-O
This addin adds an 'On Error Goto Err_Init' to the beginning of the routine, and an 'exit function', 'exit sub', or 'exit property' statement plus the error handling code at the bottom. To add error handling to a routine, place the cursor anywhere in the routine code, and choose 'Add Error Handling' from the 'Add-Ins' menu.
The code this routine adds, looks like this:

Exit (sub, function, or property here)
Err_Init:
HandleError CurrentModule, "(your routine name here)", Err.Number, Err.Description

Note that it will automatically determine which sort of 'exit' statement is necessary, and what the name of the current procedure is, and pass the procedure name to the error handler.

Rate Add Error Handling

Upload

Download this snippet    Add to My Saved Code

Add Error Handling Comments

No comments have been posted about Add Error Handling. Why not be the first to post a comment about Add Error Handling.

Post your comment

Subject:
Message:
0/1000 characters