Beginner Error Handler Question

Posted on 2006-04-15
Last Modified: 2010-05-01
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!
Question by:JohnDoeSr
    LVL 46

    Expert Comment

    by:Wayne Taylor (webtubbs)
    Hello JohnDoeSr,

    When I'm debugging my code, I add line numbers to aid in tracking down the error. MZTools ( ) 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.


    LVL 10

    Assisted Solution

    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.
    LVL 9

    Accepted Solution

    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
    LVL 13

    Expert Comment



    instead of "resume", use "resume next"

    that will skip the errorneous code (eg divide by 0)
    and execute the next line

    Author Comment

    Thanks a bunch. Both of those answers helped a ton.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now