简体   繁体   中英

Fetch thrown and swallowed Exception from CurrencyManager

The .NET Windows Forms CurrencyManager swallows exceptions that are thrown while navigating (see "Bug in CurrencyManager.OnPositionChanged - eats exceptions" on MSDN Social ).

I, however, need to catch or fetch an exception that may be thrown in a CurrentChanged event handler. Is there a way to get it? Subscribing BindingComplete and reading e.Exception does not help.

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

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

At the moment, the user gets no feedback when saving the old item fails. Therefore I need a way to get the exception.

Cheers Matthias

You could try to fetch it through: AppDomain.CurrentDomain.FirstChanceException

Simple example code:

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM