If On Error Goto 0
is called in a VB6 Sub, will this switch off error handling even when control goes back to the calling Function/Sub?
EDIT: If I have output in an error-handler block that is showing an error number of 0, what does that indicate?
If On Error Goto 0
is called in a VB6 Sub, will this switch off error handling even when control goes back to the calling Function/Sub?
EDIT: If I have output in an error-handler block that is showing an error number of 0, what does that indicate?
No.
http://www.vb-helper.com/tut6.htm
When a program encounters an error, Visual Basic checks to see if an error handler is presently installed in the current routine. If so, control passes to that error handler. If no error handler is in effect, Visual Basic moves up the call stack to the calling routine to see if an error handler is currently installed there. If so, the system resumes execution at that error handler. If no error handler is installed in the calling routine either, Visual Basic continues moving up the call stack until it finds a routine with an error handler installed. If it runs off the top of the stack before it finds an active error handler, the program crashes.
No. The VB6 manual makes it clear that On Error Goto 0
only affects the current procedure:
On Error Goto 0
disables any enabled error handler in the current procedure.
EDIT There's now an addition to the question, which wasn't there when I posted this answer. The question is "If I have output in an error-handler block that is showing an error number of 0, what does that indicate?". For the answer, see Mike's answer.
If I have output in an error-handler block that is showing an error number of 0, what does that indicate?
This means the Err
object did not contain error information at the point in the code where you checked the Err.Number
property. This can happen for a number of different reasons:
Err
object has been explicitly cleared by a previous call to Err.Clear
Err
object was cleared by calling On Error Goto
. An On Error Goto
statement will clear the current Err
objectErr
object was cleared by a Resume X
statement. Unlike a normal Goto X
statement, a Resume
will clear the current Err
object (and raise an error of its own if the Err
object was already empty)You forgot to exit the current Sub
/Function
/Property
prior to reaching the error handler, for example:
Public Sub SampleRoutine
On Error Goto ErrorHandler
DoSomething
DoSomethingElse
' You need an Exit Sub here so that the code does not reach the error handler'
'in the event no error occurs.'
ErrorHandler:
MsgBox Err.Number & "-" & Err.Description
End Sub
This is a pretty common mistake in my experience. If you don't explicitly exit the routine before reaching the error handler label, the code in the error handler will still run even when no errors occur. In this case, Err.Number
will be 0 since no error occurred.