从CurrencyManager获取抛出并吞下Exception

时间:2011-06-24 08:35:28

标签: .net winforms exception data-binding currencymanager

.NET Windows窗体CurrencyManager吞下导航时抛出的异常(请参阅"Bug in CurrencyManager.OnPositionChanged - eats exceptions" on MSDN Social)。

但是,我需要捕获或获取可能在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
}

目前,当保存旧项目失败时,用户不会收到任何反馈。因此,我需要一种方法来获得异常。

干杯 的Matthias

1 个答案:

答案 0 :(得分: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");
        }
    }
}