This documentation is archived and is not being maintained.

10.10.2 Unstructured Exception-Handling Statements

Visual Studio .NET 2003

Unstructured exception handling is a method of handling errors by indicating statements to branch to when an exception occurs. Unstructured exception handling is implemented using three statements: the Error statement, the On Error statement, and the Resume statement. For example:

Imports System

Module Test
    Sub ThrowException()
        Error 5
    End Sub

    Sub Main()
        On Error Goto GotException

        ThrowException()
        Exit Sub

GotException:
        Console.WriteLine("Caught exception!")
        Resume Next
    End Sub
End Module

When a method uses unstructured exception handling, a single structured exception handler is established for the entire method that catches all exceptions. The method then keeps track of the most recent exception-handler location and the most recent exception that has been thrown. At entry to the method, the exception-handler location and the exception are both set to Nothing. When an exception is thrown in a method that uses unstructured exception handling, a reference to the exception object is stored in the object returned by the function Microsoft.VisualBasic.Information.Err.

UnstructuredErrorStatement ::=
   ErrorStatement |
   OnErrorStatement |
   ResumeStatement

See Also

10.10.2.1 Error Statement | 10.10.2.2 On Error Statement | 10.10.2.3 Resume Statement | 10.10.1 Structured Exception-Handling Statements | 10.10 Exception-Handling Statements | Unstructured Exception Handling (Visual Basic Language Concepts)

Show: