2011-01-14 7 views

답변

2

예 그것은 당신의 응용 프로그램의 아키텍처에 따라


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

의 AppDomain.UnhandledException. 예를 들어, MVC 아키텍처를 수행하면 책임 패턴 패턴 [GOF]을 알고 있거나 모든 유형의 실행 파일을 처리하는 것을 선호한다면 controler.if 파일에 있어야합니다. 그렇지 않으면 앱에 대해 자세히 알려주세요.