Error Handle in VB5


If i want to avoid the VB5 program terminated abnormally when hit some system error, how am i going to do that?

Currently, in order to get 100% coverage of the error handling, i put the Error handling routine code in each and every control's event at all form.  This is a very tedious job.  Is there any better way to do that?

For your information, i'm using MDI form as my main form.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This is an excerpt from the VB5 Help page for "On Error"...

"On Error Enables an error-handling routine and specifies the location of the routine within a procedure; can also be used to disable an error-handling routine."

Unfortunately, since On Error only deals with error events at the procedure level, you need to perform an on error event for each event procedure call.

For example, take the following code:

   Sub ForceError()
      Error 57
   End Sub
   Private Sub Command1_Click()
      On Error Resume Next
   End Sub
   Private Sub Command2_Click()
   End Sub
   Private Sub Form_Load()
       On Error Resume Next
   End Sub

The subroutine ForceError causes an I/O error to be raised. and I call ForceError when the form loads, when Command1 is clicked and when Command2 is clicked.

Notice that there is no error handler in the actual ForceError subroutine.

When the form loads, ForceError is called, but the error is ignored because the form_load error handler is in effect... The error handler is disabled when the load exits...

When Command1 is clicked, no error occurs because the Command1_click error handler is in effect...

But, when the Command2 button is clicked, there is not error handler in effect and the error is generated.

Try this code and step through the execution...

Hope this helps!

Yes, there is no work around using the error handler. You can simplify your wrok of inserting error handler using writing your own add-in which can automate the work of putting error handler code. Another way is use some tools already developed to do this. You can checkout at
Document your code with one click, Insert Procedure Headers, Comment, Error Handlers. Add Line Numbers. Pinpoint Errors in your programs, Indent your code, Track revisions in your code
VB Error Catcher: Automatic Line Numbering end error handlers, to ease the debugging of applications at run-time (no VB installed) and other useful add-ins


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.