3

I have a program that, among other things, needs to be able to refresh the contents of a directory when the user tells it to. The actual task doesn't really matter, but this is the simplest way of causing this problem to occur that I know of. If I tell it to open a directory that doesn't exist, I get the "unhandled exception" dialog in VS with a stack trace of, from outer to inner:

[External code]

Textbox PreviewKeyUp event

[External code]

ClassA's path property being set

ClassA's internal path update function being called

A call to the INotifyPropertyChanged event

[External code]

A call to the getter for ClassB's list of children

A call to ClassB's internal directory list function

And then it fails inside this internal function. I have the following in my App.xaml.cs:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
        Application.Current.Dispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
    }

But neither of their exception handlers are being called. If I run this program from outside VS, I don't get notified of an exception at all, it just breaks because of the invalid input. And yes, the Application_Startup event is being called. How can I properly "trap" this exception so I can provide a friendly error message and kill the program?

Oh, and if I put a try/catch anywhere up the call stack past an External Code call, it doesn't catch it, either.

[edit]

After some searching, I'm pretty sure this is a side effect of WPF's binding system. Because the DirectoryInfo is being created successfully (even on a directory that doesn't exist), the error doesn't occur until a binding goes to retrieve the value - and WPF eats binding exceptions. I'm leaving this open in case anyone has any further ideas, but I think I the best I can do is abandon lazy-loading if I think it can lead to exceptions, at least until the application is more proven.

JustABill
  • 1,562
  • 15
  • 20
  • Can you show the code that actually performs the directory refreshing? And it would really help if you'd posted the actual exception being thrown. – Oleg Tarasov May 07 '10 at 05:49
  • The code that's failing is a call to DirectoryInfo.GetDirectories(), and the exception is that the directory doesn't actually exist, but that's not really the point, I'm more interested in why I can't catch *any* exception like this. – JustABill May 07 '10 at 14:35

1 Answers1

-1

Try attaching to the apps DispatcherUnhandledException.

private void Application_Startup(object sender, StartupEventArgs e)
{
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    Application.Current.Dispatcher.UnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Dispatcher_UnhandledException);
    this.DispatcherUnhandledException += ...
}
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
  • I'd actually tried that first, and still nothing. Besides, I'm pretty sure that that event is just an alias for the Application.Current.Dispatched.UnhandledException event. – JustABill May 07 '10 at 14:34
  • the DispatcherUnhandledException uses the internal Dispatcher object and adds the handler to the UnhandledException event, although it does secure the add/remove happens on the UI Thread there is no further difference. Take a look at the DispatcherUnhandledException using a reflection tool. – quadroid Apr 30 '14 at 16:01
  • what worked for me was similar: https://stackoverflow.com/questions/390838/wpf-dispatcherunhandledexception-does-not-seem-to-work – DAG Sep 10 '20 at 18:37