2013-05-23 2 views
8

모든 처리되지 않은 예외를 포착하는 작은 라이브러리를 작성 중이며 사용자에게 예외를 개발자에게 보낼 기회를 제공하는 작은 대화 상자 (NF의 일반적인 대화 상자와 유사 함)를 보여줍니다. 이렇게하려면,이 같은 응용 프로그램 도메인의 UnhandledException 이벤트를 사용UnhandledException 이벤트가 작동하지 않습니까?

app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => 
     { 
      ExceptionHandler handler = new ExceptionHandler((Exception)e.ExceptionObject, ExEntry); 
      UnhandledExceptionListened(handler); 
      if (Properties.Settings.Default.ShowStandardExceptionDialog) 
      { 
       ExceptionDialog exdialog = new ExceptionDialog(handler); 
       exdialog.ShowDialog(); 
      } 
     }; 

ExceptionHandler 및 ExEntry 내 도서관의 클래스입니다. 하지만 : 예외가 발생하면 컴파일러가 Lambda-Expression으로 점프하고 첫 번째 코드 줄을 디버깅 한 다음 나머지 람다를 처리하지 않고 이전에 발생한 오류를 표시합니다. 하지만 난 그냥 쓰는 경우 :

app.UnhandledException += (object sender, UnhandledExceptionEventArgs e) => 
     { 
       ExceptionDialog exdialog = new ExceptionDialog(handler); 
       exdialog.ShowDialog(); 
     }; 

그것을 완벽하게 작동합니다. 왜 이것이 작동하지 않는지에 대한 아이디어가 있습니까?

+1

try catch에서 최상위 코드를 감싸고 catch 지점에 감시 점을 넣습니다. 예외가 있습니까? 그렇다면 무엇입니까? – TheKingDave

+0

테스트 단위에서 Exception Handler 생성을 테스트하는 것은 어떨까요? 무슨 일이야? –

답변

3

두 가지 이유가있을 수 있습니다.

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 

또 다른 당신이 ThreadException를 처리하지 않았 음을 야기 할 수 있으며 throw 된 예외가 처리되지 않은 예외가 있지만, 스레드 예외가 아니었다 : 가능

하나는 제대로 UnhandledExceptionMode를 설정하지 않아도된다는 것입니다. 이 작동하지 않는 내 경험에

Application.ThreadException+= 
    new ThreadExceptionEventHandler(Log.WriteThreadException); 

AppDomain.CurrentDomain.UnhandledException+= 
    new UnhandledExceptionEventHandler(Log.WriteUnhandledException); 

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 

Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false); 
Application.Run(new Form1()); 
1

: 다음

예를 들어, 당신은 당신의 시나리오에 따라 수정해야 할 것입니다

public partial class Form1 : Form 
    { 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1Load(object sender, EventArgs e) 
    { 
     Application.ThreadException += ApplicationThreadException; 
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
     AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; 
    } 

    void ApplicationThreadException(object sender, ThreadExceptionEventArgs e) 
    { 
     //throw new NotImplementedException(); 
    } 

    void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
     //throw new NotImplementedException(); 
    } 
    } 

을하지만이 작업을 수행합니다

[STAThread] 
static void Main() 
{ 

    Application.ThreadException += ApplicationThreadException; 
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 
    AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; 

    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new Form1()); 
} 

static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    //throw new NotImplementedException(); 
} 

static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) 
{ 
    //throw new NotImplementedException(); 
} 

Application.Run() 전에 글로벌 처리기를 설정해야 할 수도 있습니다

관련 문제