私は.NET の信頼性機能について読んでおり、探索する次のクラスを作成しました。ExecuteCodeWithGuaranteedCleanup
class Failing
{
public void Fail()
{
RuntimeHelpers.PrepareConstrainedRegions();
try
{
}
finally
{
RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(Code, Cleanup, "fail");
}
}
private void Code(object message)
{
// Some code in here that will cause an exception...
}
private void Cleanup(object message, bool something)
{
Console.WriteLine(message);
Console.ReadLine();
}
}
メソッドのさまざまなコード本体を試しましたCode
。これらとその実行結果を以下に示します
OutOfMemoryException
-が呼び出されCleanup
ない原因
List<string> ss = new List<string>();
while (true)
{
string s = new string('x', 1000000);
ss.Add(s);
}
StackOverflowException
-が呼び出されCleanup
ない原因
Code(message); // recursive call
ExecutionEngineException
-が呼び出されCleanup
ない原因
Environment.FailFast(message.ToString());
原因ThreadAbortException
-Cleanup
が呼び出されます (ただし、通常try...finally
はこの例外をキャッチすることもできます)
Thread.CurrentThread.Abort();
だから質問は
- 私は
ExecuteCodeWithGuaranteedCleanup
正しく使用していますか? ExecuteCodeWithGuaranteedCleanup
実際に役立つのはいつですか?