共用方式為


try-finally (C# 參考)

finally區塊適合用來清除任何配置的資源中, 區塊中,並執行任何程式碼必須執行,即使發生例外狀況try區塊。 一般而言,陳述式中的finally離開控制項時,執行區塊try陳述式中,控制項的傳輸是否正常執行,執行的結果就會發生break, continue, goto,或return陳述式,或不足的例外狀況傳用的try陳述式。

在處理了例外,相關的finally區塊保證會執行。 不過,如果例外狀況未處理,執行finally區塊是依賴觸發例外狀況回溯作業的方式。 反而是取決於您的電腦設定項目。 如需詳細資訊,請參閱在 CLR 的未處理例外狀況處理。

通常,當未處理的例外狀況結束應用程式,是否finally執行區塊時並不重要。 不過,如果您有陳述式finally區塊必須執行即使是在這種情況,其中一個解決方案是加入catch區塊中, try-finally陳述式。 或者,您就可以知道可能會在擲回的例外狀況try封鎖的try-finally陳述式的呼叫堆疊之上。 也就是攔截到例外狀況,在方法中呼叫方法,其中包含try-finally陳述式,或在方法中呼叫該方法中,或在呼叫堆疊中的任何方法。 如果未攔截到例外狀況時,執行finally區塊,取決於作業系統是否選擇觸發例外狀況回溯作業。

範例

下列範例中,在無效轉換陳述式會導致System.InvalidCastException例外狀況。 未處理的例外。

public class ThrowTestA
{
    static void Main()
    {
        int i = 123;
        string s = "Some string";
        object obj = s;

        try
        {
            // Invalid conversion; obj contains a string, not a numeric type.
            i = (int)obj;

            // The following statement is not run.
            Console.WriteLine("WriteLine at the end of the try block.");
        }
        finally
        {
            // To run the program in Visual Studio, type CTRL+F5. Then 
            // click Cancel in the error dialog.
            Console.WriteLine("\nExecution of the finally block after an unhandled\n" +
                "error depends on how the exception unwind operation is triggered.");
            Console.WriteLine("i = {0}", i);
        }
    }
    // Output:
    // Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
    //
    // Execution of the finally block after an unhandled
    // error depends on how the exception unwind operation is triggered.
    // i = 123
}

在下列範例中,從例外狀況TryCast方法會攔截在呼叫堆疊之上,得更遠的方法。

public class ThrowTestB
{
    static void Main()
    {
        try
        {
            // TryCast produces an unhandled exception.
            TryCast();
        }
        catch (Exception ex)
        {
            // Catch the exception that is unhandled in TryCast.
            Console.WriteLine
                ("Catching the {0} exception triggers the finally block.",
                ex.GetType());

            // Restore the original unhandled exception. You might not
            // know what exception to expect, or how to handle it, so pass 
            // it on.
            throw;
        }
    }

    public static void TryCast()
    {
        int i = 123;
        string s = "Some string";
        object obj = s;

        try
        {
            // Invalid conversion; obj contains a string, not a numeric type.
            i = (int)obj;

            // The following statement is not run.
            Console.WriteLine("WriteLine at the end of the try block.");
        }
        finally
        {
            // Report that the finally block is run, and show that the value of
            // i has not been changed.
            Console.WriteLine("\nIn the finally block in TryCast, i = {0}.\n", i);
        }
    }
    // Output:
    // In the finally block in TryCast, i = 123.

    // Catching the System.InvalidCastException exception triggers the finally block.

    // Unhandled Exception: System.InvalidCastException: Specified cast is not valid.
}

如需有關finally,請參閱 try-catch-最後

C# 也包含 using 陳述式,它提供方便的語法與相同的功能try-finally陳述式。

C# 語言規格

如需詳細資訊,請參閱 C# 語言規格。語言規格是 C# 語法和用法的限定來源。

請參閱

工作

HOW TO:明確擲回例外狀況

參考

C# 關鍵字

請試著、 攔截,並擲回的陳述式 (C++)

例外狀況處理陳述式 (C# 參考)

throw (C# 參考)

try-catch (C# 參考)

概念

C# 程式設計手冊

其他資源

C# 參考