Link to home
Start Free TrialLog in
Avatar of chee68
chee68

asked on

Error Handle in VB5

Hi,

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.

Thanks

chee
Avatar of mcrider
mcrider

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
      ForceError
   End Sub
   Private Sub Command2_Click()
      ForceError
   End Sub
   Private Sub Form_Load()
       On Error Resume Next
       ForceError
   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!


Cheers!
ASKER CERTIFIED SOLUTION
Avatar of VBGuru
VBGuru
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial