17.7 The Finally StatementIn some instances, throwing an exception and unwinding the stack can create a problem. For example, if you have opened a file or otherwise committed a resource, you might need an opportunity to close the file or flush the buffer. If you must take some action, such as closing a file, regardless of whether an exception is thrown, you have two strategies to choose from. One approach is to enclose the dangerous action in a try block and then to perform the necessary action (e.g., close the file) in both the catch and try blocks. However, this is an ugly duplication of code, and it's error prone. VB.NET provides a better alternative in the finally block. The code in the finally block is guaranteed to be executed regardless of whether an exception is thrown. You create a finally block with the keyword Finally , and it ends with the End Try statement. A finally block can be created with or without catch blocks, but a finally block requires a try block to execute. It is an error to exit a finally block with Exit , Throw , Return , or Goto . The TestFunc( ) method in Example 17-5 simulates opening a file as its first action. The method then undertakes some mathematical operations, and then the file is closed. It is possible that sometime between opening and closing the file, an exception will be thrown. If this were to occur, it would be possible for the file to remain open . The developer knows that no matter what happens, at the end of this method the file should be closed, so the file close function call is moved to a finally block, where it will be executed regardless of whether an exception is thrown. Example 17-5 uses a finally block. Example 17-5. Using a Finally blockOption Strict On Imports System Namespace ExceptionHandling Class Tester Public Sub Run( ) Try Console.WriteLine("Open file here") Dim a As Double = 5 Dim b As Double = 0 Console.WriteLine("{0} / {1} = {2}", a, b, DoDivide(a, b)) Console.WriteLine("This line may or may not print") ' most derived exception type first Catch e As System.DivideByZeroException Console.WriteLine("DivideByZeroException caught!") Catch Console.WriteLine("Unknown exception caught!") Finally Console.WriteLine("Close file here.") End Try End Sub 'Run ' do the division if legal Public Function DoDivide( _ ByVal a As Double, ByVal b As Double) As Double If b = 0 Then Throw New System.DivideByZeroException( ) End If If a = 0 Then Throw New System.ArithmeticException( ) End If Return a / b End Function 'DoDivide Shared Sub Main( ) Console.WriteLine("Enter Main...") Dim t As New Tester( ) t.Run( ) Console.WriteLine("Exit Main...") End Sub 'Main End Class 'Tester End Namespace 'ExceptionHandling Output: Enter Main... Open file here DivideByZeroException caught! Close file here. Exit Main... In Example 17-5, one of the catch blocks from Example 17-4 has been eliminated to save space, and a finally block has been added. Whether or not an exception is thrown, the finally block is executed; thus, in both examples, the following message is output: Close file here. Of course, in a real application, you would actually open the file in the try block, and you'd actually close the file in the finally block. The details of file manipulation have been eliminated to simplify the example. |