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?
VBGuruConnect With a Mentor Commented:
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

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!

All Courses

From novice to tech pro — start learning today.