try-finally (C# リファレンス)
finally ブロックは例外が try ブロックで発生します。Try ブロックにガベージ コレクションを実行する必要があるコードを実行するのに便利ですリソースをクリーンアップします。通常finally ブロックのステートメントはコントロールが try のステートメントから制御の移動は例外の反映 breakcontinuegotoまたは return ステートメントの実行の正常実行の結果として実行または保持するかどうか try のステートメントをときに実行されます。
例外処理ではfinally に関連付けられたブロックが実行されることが保証されます。ただし例外を処理する場合finally ブロックの実行は例外が発生する操作をどのようにアンワインドするかによって異なります。これはコンピューターのセットアップ方法にあるかによって異なります。詳細については" " を参照してください。Unhandled Exception Processing in the CLR
finally ブロックが続きます通常かどうかハンドルされない例外がアプリケーションを終了すると実行は重要ではありません。ただしこの状況で実行する必要がある finally ブロックのステートメントがある場合は第 1 の解決方法は try-finally のステートメントに catch ブロックを追加します。また呼び出し履歴の最上位 try-finally のステートメントの try ブロックによりもスローされる可能性のある例外をキャッチできます。つまりこのメソッドを呼び出すと呼び出し履歴のメソッドの 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-finally を参照してください。
C# にはステートメントを使用します。 が含まれています try-finally のステートメントと同じ機能の便利な構文が提供されます。
C# 言語仕様
詳細については、「C# 言語仕様」を参照してください。言語仕様は、C# の構文と使用法に関する信頼性のある情報源です。
参照
処理手順
関連項目
try、キャッチしてスローします (C++)をステートメント