• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 150
  • Last Modified:

Beginner Error Handler Question

As my beginners VB book instructs, I use 'On Error Goto ErrorCatcher12' statements to catch errors. I then follow up just above the 'end sub' line with the corresponding error handler label like"

exit sub
If Err.Number = 91 Then
    sleep 4
    Goto Label4
End If

The problem with this is that if there is an error triggered that is other then err.number 91, i'm screwed! I've gone so far as to replace Resume with Msgbox(err.num & " " & err.description) just to see what kind of other error is triggered but then when running the program (during design) I don't get that convenient error box that asks if I'd like to DEBUG or END.

I hope I've explained it clearly enough. Thanks!
2 Solutions
Wayne Taylor (webtubbs)Commented:
Hello JohnDoeSr,

When I'm debugging my code, I add line numbers to aid in tracking down the error. MZTools ( http://www.mztools.com/ ) has some great tools for this purpose and free!

Then once the line numbers have been added, you simple need to do something like this to retreive the error details

MsgBox "An Error occurred at line number : " & Erl & vbLf & vbLf & err.Number & " - " & err.Description

This will give the exact line the error occured on, using Erl.


In the VB IDE go to the options menu. There is a tab in there where you can tell VB to either "Break on all errors" or "Break on unhandled errors". You may find the first more useful while trying to track down errors, the second when trying to test your error handling code.
If Err.Number = 91 Then
    sleep 4
    Goto Label4

' put as many (or few) of the ElseIfs as you need to handle known errors
Elseif err.number = <some other number you want to handle> then
    <handle the error>

' these are the errors that can't be handled, so let the app die
    err.raise err.number & ", " & err.description
End If


instead of "resume", use "resume next"

that will skip the errorneous code (eg divide by 0)
and execute the next line
JohnDoeSrAuthor Commented:
Thanks a bunch. Both of those answers helped a ton.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now