error handling

Posted on 2005-05-17
Last Modified: 2010-05-01
Hello All,
                 I have a question regarding error trapping. I observe that sometimes error hadlers extend thier power into subs they call. That makes it harder to work with particular errors . How do I ensure that error handlers work only locally, that is, in a procedure/function it's declared and do not cross over to any other subs/functions that they may be calling.  

Thank you.
Question by:Autofreak
    LVL 85

    Accepted Solution

    Before you call your subs/functions you can turn off error trapping and then turn it back on when the call returns.

    To turn off error handling use:

        On Error Goto 0

    Then turn it back on with:

        On Error Goto someLabelName
    LVL 5

    Assisted Solution

    Best bet is to error trap everything.
    For example if Sub1 has an error handler and Sub1 calls Sub2, if Sub2 does not have its own error routine, the error will be returned to Sub1, making it very difficult to debug.  If Sub2 has its own error routine then the error will be handled in the place that it occurred.
    LVL 5

    Assisted Solution

    You can add something like this to each sub or function (just replace Exit Sub with Exit Function).  It won't give you the line numbers in the error but this will be the best you can do without using additional subs and such.

    On Error Goto ErrHandler

    '**** Insert your code here

    Exit Sub

         msgbox "Error Description: " & err.description & vbcrlf & "Error Number: " & err.number
         exit sub '**** or change this to Resume Next if you want to try the next line

    Author Comment

    Thanks Guys, each answer is helpfull in its own way. Thanks to you i became a better trapper, I hope :)

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    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…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now