Using Finally in

By: Steven Holzner Printer Friendly Format    

The code in the Finally block, if there is one, is always executed in a TryCatchFinally statement, even if there was no exception, and even if you execute an Exit Try statement. This allows you to deallocate resources and so on; here's an example with a Finally block:

Module Module1
    Sub Main()
        Dim int1 = 0, int2 = 1, int3 As Integer
            int3 = int2 / int1
            System.Console.WriteLine("The answer is {0}", int3)
        Catch e As System.OverflowException
            System.Console.WriteLine("Exception: Arithmetic overflow!")
        Catch e As System.ArgumentException
            System.Console.WriteLine("Exception: Invalid argument value!")
        Catch e As System.ArgumentOutOfRangeException
            System.Console.WriteLine("Exception: Argument out of range!")
            System.Console.WriteLine("Execution of sensitive code " & _
                "is complete")
        End Try
    End Sub
End Module

And here's what you see when you execute this console application:

Exception: Arithmetic overflow!
Execution of sensitive code is complete

Most Viewed Articles (in )

Latest Articles (in

Comment on this tutorial