O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Try...Catch...Finally Statement

Syntax

Try
   tryStatements
[Catch1 [exception [As type]] [When expression] 
   catchStatements1
[Exit Try]

Catch2 [exception [As type]] [When expression] 
   catchStatements2
[Exit Try]
. . .
Catchn [exception [As type]] [When expression] 
   catchStatementsn]
[Exit Try]

[Finally
   finallyStatements]
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, 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 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 the Try block and are the statements that VB monitors for errors.

  • The Catchblocks, of which there can be more than one, contain code that is executed in response to VB “catching” a particular type of error within the Try block. Thus, the Catch blocks consist of the error-handlers for the Try block.

  • The phrases exception [As type ] and [When expression ] are referred to as filters in the VB.NET documentation. In the former case, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required