C# Windows フォーム アプリケーションで未処理の例外をすべてキャプチャしようとしています。次のコードをProgram.cs
ファイルに追加しましたが、例外がキャプチャされず、NullReferenceException
. 私は何を間違っていますか?
static void Main()
{
System.Windows.Forms.Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
var form = new MainForm();
form.ShowDialog();
}
private static void HandleUnhandledException(Object o)
{
// TODO: Log it!
Exception e = o as Exception;
if (e != null)
{
}
}
private static void OnUnhandledException(Object sender, UnhandledExceptionEventArgs e)
{
HandleUnhandledException(e.ExceptionObject);
}
private static void OnGuiUnhandedException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
HandleUnhandledException(e.Exception);
}
編集:プログラムが Visual Studio の外部で実行されている場合は例外をキャッチできますが、Visual Studio からデバッグする場合は例外をキャッチできません。 ?