Udostępnij za pośrednictwem


How to: Use Finally Blocks

When an exception occurs, execution stops and control is given to the closest exception handler. This often means that lines of code you expect to always be called are not executed. Some resource cleanup, such as closing a file, must always be executed even if an exception is thrown. To accomplish this, you can use a finally block. A finally block is always executed, regardless of whether an exception is thrown.

The following code example uses a try/catch block to catch an ArgumentOutOfRangeException. The Main method creates two arrays and attempts to copy one to the other. The action generates an ArgumentOutOfRangeException and the error is written to the console. The finally block executes regardless of the outcome of the copy action.

Example

Imports System

Class ArgumentOutOfRangeExample
    Public Shared Sub Main()
        Dim array1() As Integer = {0, 0}
        Dim array2() As Integer = {0, 0}

        Try
            Array.Copy(array1, array2 , -1)
        Catch e As ArgumentOutOfRangeException
            Console.WriteLine("Error: {0}", e)
        Finally
            Console.WriteLine("This statement is always executed.")
        End Try
    End Sub
End Class
using System;

class ArgumentOutOfRangeExample
{
    public static void Main()
    {
        int[] array1 = {0, 0};
        int[] array2 = {0, 0};

        try
        {
            Array.Copy(array1, array2, -1);
        }
        catch (ArgumentOutOfRangeException e)
        {
            Console.WriteLine("Error: {0}", e);
        }
        finally
        {
            Console.WriteLine("This statement is always executed.");
        }
    }
}
using namespace System;

ref class ArgumentOutOfRangeExample
{
public:
    static void Main()
    {
        array<int>^ array1 = {0, 0};
        array<int>^ array2 = {0, 0};

        try
        {
            Array::Copy(array1, array2, -1);
        }
        catch (ArgumentOutOfRangeException^ e)
        {
            Console::WriteLine("Error: {0}", e);
        }
        finally
        {
            Console::WriteLine("This statement is always executed.");
        }
    }
};

int main()
{
    ArgumentOutOfRangeExample::Main();
}

See Also

Tasks

How to: Use the Try/Catch Block to Catch Exceptions

How to: Explicitly Throw Exceptions

How to: Create User-Defined Exceptions

Other Resources

Exception Handling Fundamentals