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

error handling

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.
3 Solutions
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
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.
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
AutofreakAuthor Commented:
Thanks Guys, each answer is helpfull in its own way. Thanks to you i became a better trapper, I hope :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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