Try...Catch...Finally Statement

   
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] . . . Catch   n   [   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 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 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, exception is either a variable of type Exception, which is the base class that "catches" all exceptions, or a variable of one of Exception's derived classes. The When filter is typically used with user -defined errors. (See the upcoming example.)

  • The Exit Try statement is used to break out of any portion of a Try...Catch...Finally block.

  • The optional finallyStatements code block is executed regardless of whether an error occurs (or is caught), unless an Exit Try statement is executed.

  • Multiple Catch statements can be used. However, only the first Catch statement to be true is executed. This means that multiple Catch statements should be ordered from most specific to most general, with a Catch block handling errors of type System.Exception occurring last.

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.

 Try     Dim sInput As String     sInput = Inputbox("Enter a number.")     If Not IsNumeric(sInput) Then         Err(  ).Raise(1)     End If Catch When Err.Number = 1     Msgbox("Error1") End Try 

Programming Tips and Gotchas

As with unstructured error handling, VB may pass an error up the call stack when using structured error handling. This happens in the following situations:

  • If an error occurs within a Try block that is not handled by an existing Catch block.

  • If an error occurs outside any Try block (provided, of course, that no On Error -style error handlers are active).

VB.NET/VB 6 Differences

Structured exception handling using the Try...Catch...Finally construct is new to VB.NET. It replaces unstructured error handling using the On Error statement, which continues to be supported in VB.NET.

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net