2011-06-24 18 views
0

.NET Windows窗體CurrencyManager吞下導航時拋出的異常(請參閱"Bug in CurrencyManager.OnPositionChanged - eats exceptions" on MSDN Social)。從CurrencyManager中提取併吞服異常

但是,我需要捕獲或獲取可能在CurrentChanged事件處理程序中拋出的異常。有沒有辦法得到它?訂閱BindingComplete和閱讀e.Exception沒有幫助。

bindingSource.MoveLast(); 
// exception isn't thrown up to here 

private void bindingSource_CurrentChanged(object sender, EventArgs e) 
{ 
    // save old, throws exception 
} 

此時用戶在保存舊項目失敗時不會得到任何反饋。因此我需要一種方法來獲取異常。

乾杯 馬蒂亞斯

回答

1

你可以嘗試通過獲取它:AppDomain.CurrentDomain.FirstChanceException

簡單的示例代碼:

using System; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      AppDomain.CurrentDomain.FirstChanceException += (s, e) => Console.WriteLine(String.Format("Exception thrown: {0}", e.Exception.GetType())); 

      try 
      { 
       ThrowException(); 
      } 
      catch(InvalidProgramException) 
      { 
       // mjam mjam 
      } 

      Console.Read(); 
     } 

     private static void ThrowException() 
     { 
      throw new InvalidProgramException("broken"); 
     } 
    } 
} 
+0

謝謝,做這項工作。我將這個異常封裝在一個'WrappedException'類中,以區分我想要的異常和其他異常('is'運算符)。 –