.netwinformsexceptiondata-bindingcurrencymanager

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


Solution

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