2010-08-17 126 views
3

捕獲wpf應用程序中的錯誤的最佳方法是什麼?我已經增加了以下我app.xaml.cs:在客戶端機器上調試WPF

void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) 
{ 

    var stringBuilder = new StringBuilder(); 
    stringBuilder.AppendFormat("{0}\n", e.Exception.Message); 
    stringBuilder.AppendFormat(
      "Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId); 

    // attempt to save data 
    var result = MessageBox.Show(
        "Application must exit:\n\n" + stringBuilder.ToString() + "\n\nSave before exit?", 
        "app", 
        MessageBoxButton.YesNo, 
        MessageBoxImage.Error); 
    if (result == MessageBoxResult.Yes) 
    { 
     MessageBox.Show(e.Exception.InnerException.ToString()); 
     MessageBox.Show(e.Exception.InnerException.Message.ToString()); 

    } 

    // Return exit code 
    this.Shutdown(-1); 

    // Prevent default unhandled exception processing 
    e.Handled = true; 
} 
private void Application_Startup(object sender, StartupEventArgs e) 
{ 
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
} 

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    Exception ex = e.ExceptionObject as Exception; 
    MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error); 
} 

但是我還是在應用程序崩潰和錯誤並不在一個消息顯示occassions。我如何調試這些類型的錯誤? Windows的事件日誌沒有顯示任何有用的信息。當然,該應用程序在IDE中運行良好,所以這也沒有什麼幫助。

回答

0

在IDE中,您可以捕獲它。轉至
調試 - >例外。

從對話框中,選中拋出的複選框公共語言運行時異常

現在,您將能夠從IDE中捕獲它。

HTH

+0

我很抱歉,但你的回答不回答我的問題:怎麼我在IDE中的客戶機上,而不是調試應用程序時,我的錯誤處理不捕獲異常,但應用程序只是說它停止工作 – internetmw 2010-08-18 23:31:54