2011-01-14 5 views

答えて

2

はいその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(); 
    } 
} 
+0

アプリケーションに複数のAppDomainsがある場合は、そのイベントをそれぞれにフックする必要があります。 –

1
[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()); 
} 
0

それはあなたのアプリケーションのアーキテクチャに依存します。たとえば、MVCアーキテクチャーを行う場合、責任パターンの連鎖[GOF]を知っているか、すべてのタイプの執行の大型ハンドラーが好きな場合は、controler.ifにあるはずです。それ以外の場合は、アプリについて詳しく教えてください。

関連する問題