ThreadExceptionドキュメントの例を見てください。
public static void Main(string[] args)
{
// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new
ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);
// Set the unhandled exception mode to force all Windows Forms errors
// to go through our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Add the event handler for handling non-UI thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
また、デバッグが容易になるため、デバッグ時に例外をキャッチしないようにすることもできます。これは多少のハックですが、そのために上記のコードを次のようにラップすることができます
if (!AppDomain.CurrentDomain.FriendlyName.EndsWith("vshost.exe")) { ... }
デバッグ時に例外がキャッチされないようにするため。
編集:ファイル名をチェックするよりもクリーンに感じるデバッガー内で実行されているアプリケーションをチェックする別の方法。
(meltedform、Kiquenet、Dougによるコメントを参照)
if(!System.Diagnostics.Debugger.IsAttached) { ... }
これにより、とは異なるデバッガーを使用する問題が回避されますvshost.exe
。