Name
Try...Catch...Finally Statement
Syntax
TrytryStatements
[Catch1 [exception
[Astype
]] [Whenexpression
]catchStatements1
[Exit Try] Catch2 [exception
[Astype
]] [Whenexpression
]catchStatements2
[Exit Try] . . . Catchn
[exception
[Astype
]] [Whenexpression
]catchStatementsn
] [Exit Try] [FinallyfinallyStatements
] End Try
-
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, ifexception
is omitted, no information about the exception will be accessible within theCatch
block.-
type
(optional) The data type of the exception to be handled by the
Catch
block. Its value can be System.Exception or any derived type. If omitted, its value defaults to System.Exception, and all exceptions will be handled.-
expression
(optional; Boolean) A logical expression that defines a condition under which the error is to be handled by the
Catch
block.
Description
Used to handle runtime errors
Rules at a Glance
The
tryStatements
, which are required, constitute theTry
block and are the statements that VB monitors for errors.The
Catch
blocks, of which there can be more than one, contain code that is executed in response to VB “catching” a particular type of error within theTry
block. Thus, theCatch
blocks consist of the error-handlers for theTry
block.The phrases
exception
[As
type
]
and[When
expression
]
are referred to as filters in the VB.NET documentation. In the former case, ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.