Partager via


Throw Statement (Visual Basic) 

Throws an exception within a procedure.

Throw expression

Part

  • expression
    Required. Provides information about the exception to be thrown. Optional when residing in a Catch statement.

Remarks

The Throw statement throws an exception that you can handle with structured exception-handling code (Try...Catch...Finally) or unstructured exception-handling code (On Error GoTo). You can use the Throw statement to trap errors within your code because Visual Basic moves up the call stack until it finds the appropriate exception-handling code.

A Throw statement with no expression can only be used in a Catch statement, in which case the statement rethrows the exception currently being handled by the Catch statement.

Example

The following code uses the Throw statement to throw an exception:

' Throws a new exception.
Throw New System.Exception("An exception has occurred.")

Requirements

Namespace: Microsoft.VisualBasic

Module: Interaction

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Tasks

How to: Check an Exception's Inner Exception

Reference

Try...Catch...Finally Statement (Visual Basic)
On Error Statement (Visual Basic)

Concepts

Structured Exception Handling Overview for Visual Basic
Unstructured Exception Handling Overview