On Error Statement


On Error Statement

     On Error { GoTo { label | 0 | -1 } | Resume Next } 


label (required, unless 0 or -1 used)

A valid label within the procedure

Description

The On Error statement enables or disables error handling within a procedure. Error handling is performed either by using a TRy...Catch...Finally statement or with an On Error statement. The On Error statement has several variations.


On Error GoTo label

When an error occurs, execution continues within the procedure at the line marked with label. A Resume statement can be used within that block of code to return to the errant section of code.


On Error GoTo 0

Disables error handling in the current procedure. This is the same as if no error handlers were ever enabled in the procedure in the first place.


On Error GoTo -1

Used inside of an error handler to make the code forget that it is inside of an error handler. It says, "Forget that the error ever happened, but don't Resume me back to the earlier part of the code; leave me here."


On Error Resume Next

When an error occurs, execution continues with the line that follows the errant line. The Err object can be examined for the error details.

Usage at a Glance

  • If you have no error handling in your procedure, or if error handling is disabled, the VB runtime engine will trace back through the call stack until a procedure is reached where error handling is enabled. In that case, the error will be handled by that procedure. However, if no error handler can be found in the call stack, a runtime error occurs, and program execution is halted. (New in 2005. Visual Basic 2005 also allows you to include a global error handler that catches errors not caught by another error handler.)

  • Use of the On Error Resume Next statement can cause legitimate errors to go unnoticed. Use it with care.

  • The following code demonstrates the typical use of the On Error statement.

         Sub SomeProcedure(  )        On Error Goto ErrorHandler        ' ... other code goes here ...     CleanupCode:        ' ... cleanup code goes here ...        Exit Sub     ErrorHandler:        ' ... error handling code goes here ...        Resume CleanupCode     End Sub 

    If cleanup code isn't required within the procedure, the CleanupCode section can be removed (keep the Exit Sub statement), and the error handler can use Resume Next or Exit Sub to complete the error.

  • If the calling procedure is responsible for handling the error, the following type of code will do minimal error handling before reporting the error up the call stack.

         Sub SomeProcedure(  )        On Error Goto ErrorHandler        ' ... other code goes here ...        ' ... cleanup code goes here ...         Exit Sub     ErrorHandler:        ' ... minimal error handling code goes here ...        Err.Raise ...   ' passes the error up        Exit Sub     End Sub 

  • For more information on both unstructured error handling and structured exception handling, see Chapter 11.

See Also

Err Object




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net