ASP.NETでは、未処理のエラーを処理するために、global.asax内でApplication_Error
を使用できます。Windowsフォームプロジェクトの中央エラーハンドラを配置する場所
Windowsフォームには同等の機能はありますか?
ASP.NETでは、未処理のエラーを処理するために、global.asax内でApplication_Error
を使用できます。Windowsフォームプロジェクトの中央エラーハンドラを配置する場所
Windowsフォームには同等の機能はありますか?
はいそのAppDomain.UnhandledException
using System;
using System.Security.Permissions;
public class Test {
[SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
public static void Example()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
try {
throw new Exception("1");
} catch (Exception e) {
Console.WriteLine("Catch clause caught : " + e.Message);
}
throw new Exception("2");
// Output:
// Catch clause caught : 1
// MyHandler caught : 2
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args) {
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}
public static void Main() {
Example();
}
}
[STAThread]
static void Main()
{
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.Run(new FrmMain());
}
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
MessageBox.Show("Unhandled exception: "+e.Exception.ToString());
}
それはあなたのアプリケーションのアーキテクチャに依存します。たとえば、MVCアーキテクチャーを行う場合、責任パターンの連鎖[GOF]を知っているか、すべてのタイプの執行の大型ハンドラーが好きな場合は、controler.ifにあるはずです。それ以外の場合は、アプリについて詳しく教えてください。
アプリケーションに複数のAppDomainsがある場合は、そのイベントをそれぞれにフックする必要があります。 –