|< Day Day Up >|
Structured exception handling is a style of exception handling in which exception handlers are defined for a specific block of code. A structured exception handling statement begins with the keyword Try . One or more Catch statements can follow the end of the statements in the Try block; each Catch statement handles a particular type of exception by specifying the keyword Catch , a variable to hold the exception, and the exception type to be caught. For example:
Dim x, y As Integer Try x = CInt(Console.ReadLine()) y = CInt(Console.ReadLine()) Console.WriteLine(x \ y) Catch e As OverflowException Console.WriteLine("Overflow") Catch e As DivideByZeroException Console.WriteLine("Divide by zero") Catch e As Exception Console.WriteLine(e.Message) End Try
When an exception is thrown, each catch handler in the current Try statement is examined in order from first to last to determine whether the handler handles that kind of exception. If the type of the exception is the same as (or derived from) the handler exception type, the catch handler is executed. In the preceding example, if y was zero, the integer division operation x \ y would throw a System.DivideByZeroException exception. The system would check to see whether the first Catch block handled that type of exception. Since it doesn't, it would look at the second Catch block. Since the second Catch block handles that type of exception, it would execute the code that prints Divide by Zero .
A Try statement may also specify a Finally block. The statements in a Finally block will always be executed when execution leaves the Try block, no matter whether an exception occurred or not. Finally blocks are useful for ensuring that variables are cleaned up properly when an exception occurs.
Imports System.IO Module Test Sub Main() Dim Output As FileStream = File.Open("output.txt", FileMode. _ Create) Try ' Write some values ... Finally ' Ensure the file is closed If Not Output Is Nothing Then Output.Close() End If End Try End Sub End Module
Catch blocks may also have conditional statements attached to them to provide additional conditions for handling an exception. This is done by adding the keyword When and a conditional expression that evaluates to a Boolean value. A Catch block can specify just a type (like System.Exception ) or a When clause or both. For example:
Imports System.IO Module Test Sub Main() Dim Output As FileStream = File.Open("output.txt", FileMode.Create) Dim Count As Byte Try ' Write some values For Count = 1 To 10 Output.Write(Count) Next ... Catch e As Exception When Count < 10 Console.WriteLine("Wrote only " & Count & " values.") Finally ' Ensure the file is closed Output.Close() End Try End Sub End Module
It is sometime useful to catch an exception, handle the exception in some way, and then let the exception continue propagating, to be caught by another handler. The most straightforward way to do this is just to end the catch handler by throwing the exception again.
Try ... Catch e As Exception ' Acknowledge that the exception was thrown Console.WriteLine("Exception: " & e.Message) ' Let the exception continue Throw e End Try
The problem with this is that exceptions contain information about where they were thrown from. This information can be used by a debugger to show where an exception occurred or can be used by other catch handlers to provide diagnostics about program failure. When an exception is thrown, as in the preceding example, the .NET Framework marks the exception with the current location. Any existing location information already contained in the exception is overwritten. To avoid this problem, you can specify the Throw statement without an argument in a catch handler.
Try ... Catch e As Exception ' Acknowledge that the exception was thrown Console.WriteLine("Exception: " & e.Message) ' Let the exception continue Throw End Try
This causes the current exception to be rethrown, with all location information preserved.
|< Day Day Up >|