2015-10-07 37 views
1

標題爲。未處理UnhandledException的可能原因是什麼?

我一直在谷歌上搜索周圍和閱讀threads ...

我非常肯定沒有別的地方做e.Handle停止異常冒泡至UnhandledException處理程序。

我測試我的代碼如下:

App.xaml.cs

public partial class App : Application 
{ 
    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     AppDomain.CurrentDomain.UnhandledException += 
      CurrentDomain_UnhandledException; 
     Application.Current.DispatcherUnhandledException += 
      Current_DispatcherUnhandledException; 
    } 
    private void Current_DispatcherUnhandledException(
     object sender,  
     DispatcherUnhandledExceptionEventArgs e) 
    { 
     MessageBox.Show(e.Exception.StackTrace); 
     e.Handled = true; 
    } 

    void CurrentDomain_UnhandledException(
     object sender, 
     UnhandledExceptionEventArgs e) 
    { 
     MessageBox.Show(e.ExceptionObject); 
    } 
} 

一個ViewModel屬性綁定到DataGrid的的SelectedItem:其他地方一樣,我可以

private object _selectedItem; 
public object SelectedItem 
{ 
    get 
    { 
     return this._selectedItem; 
    } 
    set 
    { 
     throw new Exception("Test"); 

     if (this._selectedItem == value) 
      return; 

     this._selectedItem = value; 

     this.RaisePropertyChanged(); 
    } 
} 

出於某種原因看到正在處理的異常,消息框顯示出來。但只有當我在DataGrid中進行選擇時,纔會觸發SelectedItem中的setter,該異常僅在Visual Studio中顯示,但未由處理程序處理......原因是什麼?

+0

VS配置爲打破一些例外。另外,我不認爲它知道你在一個完全不同的地方捕捉異常,所以它把它當作一個未處理的異常。 – Will

回答

0

有時,如果您使用TPL
即Tasks操作,並且Funcs(如果我調用BeginInvoke AppDomain.CurrentDomain.UnhandledException不會捕獲它們的異常)。

相關問題