Export (0) Print
Expand All

10.10.1 Structured Exception-Handling Statements

Visual Studio .NET 2003

Structured exception handling is a method of handling errors by declaring explicit blocks within which certain exceptions will be handled. Structured exception handling is done through a Try statement.

For example:

Imports System

Module Test
    Sub ThrowException()
        Throw New Exception()
    End Sub

    Sub Main()
        Try
            ThrowException()
        Catch e As Exception
            Console.WriteLine("Caught exception!")
        Finally
            Console.WriteLine("Exiting try.")
        End Try
    End Sub
End Module

A Try statement is made up of three kinds of blocks: try blocks, catch blocks, and finally blocks. A try block is a statement block that contains the statements to be executed. A catch block is a statement block that handles an exception. A finally block is a statement block that contains statements to be run when the Try statement is exited, regardless of whether an exception has occurred and been handled. A Try statement, which can only contain one try block and one finally block, must contain at least one catch block or finally block. It is invalid to explicitly transfer execution into a try block except from within a catch block in the same statement.

StructuredErrorStatement ::=
   ThrowStatement |
   TryStatement
TryStatement ::=
   Try StatementTerminator
   [ Block ]
   [ CatchStatement+ ]
   [ FinallyStatement ]
   End Try StatementTerminator

See Also

10.10.1.1 Finally Blocks | 10.10.1.2 Catch Blocks | 10.10.2 Unstructured Exception-Handling Statements | 10.10 Exception-Handling Statements | Throw Statement (Visual Basic Language Reference) | Try...Catch Statements (Visual Basic Language Concepts) | Structured Exception Handling (Visual Basic Language Concepts)

Show:
© 2014 Microsoft