Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to re-enable the default error handling in VB6

I have some code with various "On Error Goto" error handlers in a few places to handle some broken third party hardware. I was getting an overflow error (read from the Err variable) in a routine that doesn't have an error trap but is called by a routine that does. I always thought error traps were only valid in the routine they were declared, but it looks like an error in a subroutine can cause it to go to the calling function's error trap.

So I turned off the calling function's error trap and found my overflow and all is well. But before I did that, I spent some time trying to find a programatic way to get VB to return to its default error handling inside that routine (so I wouldn't have to modify outside code to debug), but I couldn't. The only error commands I could find:

  On Error GoTo [label]
  On Error Resume Next
  On Error Goto 0
  On Error GoTo -1

all turn on the manual error handling - is there a way to turn it off (back to the VB6 default)?

like image 400
Fred Hamilton Avatar asked Apr 10 '09 01:04

Fred Hamilton


1 Answers

This is explained thoroughly in the VB6 manual under Error Handling Hierarchy. On Error Goto 0 disables the error handler in the current procedure, not in the procedures that called it.

If an error occurs in a procedure and this procedure doesn't have an enabled error handler, Visual Basic searches backward through the pending procedures in the calls list — and executes the first enabled error handler it finds. If it doesn't encounter an enabled error handler anywhere in the calls list, it presents a default unexpected error message and halts execution.

As others have said, you can go to Tools-Options-General tab and choose Break on all errors. That effectively disables all your On Error statements - the IDE will break immediately on every error.

That can be irritating if your VB6 code throws errors as part of normal operation. For instance when you check whether a file exists, or when the user presses cancel in a common dialogue. You don't want the IDE to break every time on those lines. But you might have boilerplate error handlers in all your event handling procedures, to stop the program crashing out on unexpected errors. But they are a nuisance when you're debugging problems because the IDE doesn't break on the line with the error. One trick is to switch off those error handlers when running in the IDE, but keep them in the built executable. You do it like this.

Drop these functions into a module.

Public Function InIDE() As Boolean 
  Debug.Assert Not TestIDE(InIDE) 
End Function 

Private Function TestIDE(Test As Boolean) As Boolean 
  Test = True 
End Function 

Then you can write your error handlers like this.

Private Sub Form_Load() 
  If Not InIDE() Then On Error Goto PreventCrashes 
  <lots of code> 
  Exit Sub 

PreventCrashes: 
  <report the error> 
End Sub 

Pinched from here. Another tip - use the free add-in MZTools to automatically add these boilerplate error handlers. For production-quality code, you could go further and put an error handler in every routine to create a ghetto stack trace. You might also log the errors immediately in every error handler.

EDIT: Ant has correctly pointed out that On Error Goto -1 is a VB.Net statement and isn't valid in VB6.

EDIT: Arvo and OneNerd have written answers with some interesting discussion of emulating Finally teardown blocks in VB6 error handling. The discussion in this question is also worth a look.

like image 159
MarkJ Avatar answered Sep 16 '22 23:09

MarkJ