Try...Catch...Finally Statement | |
Syntax Try [tryStatements] [Catch [exception [As type]] [When expression] [catchStatements] [Exit Try]...] [Finally [finallyStatements]] End Try
tryStatements (optional) Program code to be executed and monitored for exceptions.
exception (optional; System.Exception or a derived type) The exception to catch. If exception is omitted, or if it is System.Exception, all exceptions will be caught. However, if exception is omitted, no information about the exception will be accessible within the Catch block.
type (optional) The data type of the exception to be handled by the Catch block. Its value can be System.Exception (to handle all possible exceptions) or any class derived from System.Exception (to handle only exceptions of that specific type). If omitted, its value defaults to System.Exception.
expression (optional; Boolean) Defines a condition under which the error matching exception is to be handled by the Catch block. If expression is TRue, the related Catch block is entered.
catchStatements (optional) Program code to be executed when a general or specific exception occurs.
finallyStatements (optional) Program code to be executed upon leaving the TRy...Catch...Finally statement for any reason, whether there was an exception or not. Description The try...Catch...Finally statement enables structured exception handling for a specific block of code. If an error or exception occurs in the tryStatements block of code, that exception is compared with the exception defined by each Catch clause. If a match is found (and the optional When expression also matches), the related catchStatements are processed. In all cases, the finallyStatements are executed just before exiting the try statement, even if the TRy statement is exited through a Return statement or any other method. Usage at a Glance The TRy statement can include any number of Catch blocks. The ExitTry statement is used to break out of any portion of a TRy...Catch...Finally block. The finallyStatements block is still executed. Exit Try is not permitted in the finallyStatements block. If multiple Catch clauses match a triggered exception, only the first matching Catch block is executed. This means that Catch blocks should be ordered from most specific to most general, with a Catch block handling errors of type System.Exception occurring last. If an error occurs within tryStatements that is not handled by a Catch block, the error is passed up the VB call stack to the next error handler, either structured or unstructured, that can handle the exception. Example The code in the following try block will raise an error if the user does not enter a number. The Catch block will catch this error. Dim numerator As Decimal Dim denominator As Decimal Dim quotient As Decimal Dim badData As Boolean = False Try numerator = CInt(InputBox("Enter the numerator.")) denominator = CInt(InputBox("Enter the denominator.")) quotient = numerator / denominator Catch ex As System.Exception ' ----- Probably a divide by zero error. MsgBox(ex.Message) badData = True End Try If (badData = False) Then MsgBox("Quotient = " & quotient) Version Differences Structured exception handling using the try...Catch...Finally construct is new to VB under .NET. It provides an alternate method of monitoring and processing errors, in addition to the unstructured On Error handling methods long a part of VB. See Also On Error Statement, Using Statement |