2011-06-24 37 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'运算符)。 –