ハンドルされない例外ハンドラは、3 番目の位置に挿入されます。シーケンスは次のとおりです。
- デバッガーは最初の例外の通知を受け取ります (接続されている場合)
- 例外ハンドラ別名。
try/catch
呼び出されます (利用可能な場合)
- 未処理の例外ハンドラ (複数形に注意) が呼び出されます (利用可能な場合)
- デバッガーは 2 回目の例外の通知を受け取ります (接続されている場合)
- システムは未処理の例外を気にします (通常: プロセスを終了します)
次の C# プログラムはそれを示しています。.NET のバージョンによっては、別の未処理の例外ハンドラーのメッセージが表示されます。これは、.NET フレームワークが例外とコール スタックを出力することです。
using System;
namespace UnhandledException
{
static class Program
{
static void Main()
{
Console.WriteLine("Please attach the debugger now and press Enter.");
Console.ReadLine();
AppDomain.CurrentDomain.UnhandledException += (sender, e) => Unhandled1();
AppDomain.CurrentDomain.UnhandledException += (sender, e) => Unhandled2();
try
{
Console.WriteLine("Throwing now.");
// Will cause a first chance, because in try/catch
throw new Exception("Any exception will do");
}
catch (Exception)
{
// Will cause first chance and second chance, because in NOT try/catch
Console.WriteLine("In catch block.");
throw;
}
}
static void Unhandled1() => Console.WriteLine("In unhandled exception handler 1");
static void Unhandled2() => Console.WriteLine("In unhandled exception handler 2");
}
}
デバッガーで必要なコマンド (WinDbg):
.symfix
.reload
sxe clr
g; *** for the breakpoint due to attaching the debugger
g; *** first chance in try/catch
g; *** first chance outside try/catch
g; *** second chance