Delen via


Unstructured Exception Handling Overview

In unstructured exception handling, you place an On Error statement at the beginning of a block of code, and it handles any errors occurring within that block. When an exception is raised in a procedure after the On Error statement executes, the program branches to the line argument specified in the On Error statement. The line argument, which is a line number or line label, indicates the exception handler location.

Sometimes a call is made from the original procedure to another procedure, and an exception occurs in the called procedure. In such cases, if the called procedure does not handle the exception, the exception propagates back to the calling procedure, and execution branches to the line argument.

Note

Unstructured error handling using On Error can degrade application performance and result in code that is difficult to debug and maintain. Structured error handling is the recommended method. For more information, see Structured Exception Handling Overview for Visual Basic.

On Error GoTo Line

The On Error GoTo Line statement assumes that error-handling code starts at the line specified in the required line argument. If a run-time error occurs, control branches to the line label or line number specified in the argument, activating the error handler*.* The specified line must be in the same procedure as the On Error GoTo Line statement; otherwise, Visual Basic generates a compiler error. The following example illustrates the use of an error handler with a line label:

Sub TestSub
   On Error GoTo ErrorHandler
      ' Code that may or may not contain errors.
   Exit Sub

   ErrorHandler:
      ' Code that handles errors.
      Resume
End Sub

The example contains an error handler named ErrorHandler. If any code in the TestSub subroutine generates an error, Visual Basic immediately executes the code that follows the ErrorHandler label. At the end of the error-handling block, the Resume statement passes control back to the line of code where the error first occurred. The rest of the subroutine then continues executing as if the error did not occur.

Note

You must place an Exit Sub statement immediately before the error-handling block. Otherwise, Visual Basic runs the error-handling code when it reaches the end of the subroutine, causing unwanted or unexpected results.

On Error Resume Next

The On Error Resume Next statement specifies that in the event of a run-time error, control passes to the statement immediately following the one in which the error occurred. At that point, execution continues. On Error Resume Next enables you to put error-handling routines where errors will occur, rather than transferring control to another location in the procedure.

Note

If your procedure calls another procedure, the On Error Resume Next statement becomes inactive during the execution of the called procedure. Therefore, you should place an On Error Resume Next statement in each called procedure that needs one. This is necessary because the Resume Next behavior applies only to the procedure containing the On Error Resume Next statement. If an unhandled error occurs in a called procedure, the exception propagates back to the calling procedure, and execution resumes on the statement following the call. In such cases, the error is not handled.

Resume also can be used on its own, outside the On Error statement. When Resume is used this way, Visual Basic returns control to the statement that caused the error. You generally use Resume after an error handler corrects the error.

Visual Basic also provides the Resume Next statement, which directs control to the line immediately following the line of code that caused the error. You might use Resume Next for cases in which an error will not cause your application to stop working. You might also use it if an error will not change the expected results of your subroutine.

Another variation on the Resume statement is Resume Line, which is similar to On Error GoTo Line. Resume Line passes control to a line you specify in the line argument. You can use Resume Line only within an error handler.

Note

When debugging your code, you must disable the On Error Resume Next statement.

On Error GoTo 0

The On Error GoTo 0 statement disables any error handler in the current procedure. If you do not include an On Error GoTo 0 statement, the error handler is still disabled when the procedure containing the exception handler ends.

Note

The On Error GoTo 0 statement is not meant to specify line 0 as the start of the error-handling code, even if the procedure contains a line numbered 0.

On Error GoTo -1

The On Error GoTo -1 statement disables any exception handlers in the current procedure. If you do not include an On Error GoTo -1 statement, the exception is automatically disabled when its procedure ends.

Note

The On Error GoTo -1 statement is not meant to specify line -1 as the start of the error-handling code, even if the procedure contains a line numbered -1.

Unstructured Exception Handler Example

In the following code, the exception handler is named DivideByZero and handles a specific error — that of attempting to divide by zero. If a different error occurs, Visual Basic raises a run-time error and stops the application.

Sub ErrorTest ()
' Declare variables.
   Dim x As Integer, y As Integer, z As Integer
   ' The exception handler is named "DivideByZero".
   On Error GoTo DivideByZero
   ' The main part of the code, which might cause an error.
   x = 2
   y = 0
   z = x \ y

   ' This line disables the exception handler.
   On Error GoTo 0
      Console.WriteLine(x & "/" & y & " = " & z)

   ' Exit the subroutine before the error-handling code.
   ' Failure to do so can create unexpected results.
   Exit Sub

   ' This is the exception handler, which deals with the error.
   DivideByZero:
   ' Include a friendly message to let the user know what is happening.
   Console.WriteLine("You have attempted to divide by zero!")

   ' Provide a solution to the error.
   y = 2

   ' The Resume statement returns to the point at which
   ' the error first occurred, so the application
   ' can continue to run.
   Resume

End Sub

See Also

Tasks

How to: Retrieve Information from an Error Object

Concepts

Err Object in Unstructured Exception Handling

Introduction to Exception Handling

Types of Errors

Choosing When to Use Structured and Unstructured Exception Handling

Reference

End Statement

Err Object (Visual Basic)

Exit Statement (Visual Basic)

On Error Statement (Visual Basic)

Resume Statement