2

I'm trying to handle any unhandled exception thrown in the application using this:

The xaml:

<Application x:Class="Updater.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
             ShutdownMode="OnExplicitShutdown"
             DispatcherUnhandledException="UnhandledException"
             >

The code:

private void UnhandledException(object sender,    DispatcherUnhandledExceptionEventArgs e)
{
    var time = DateTime.Now;
    File.AppendAllText(@"C:\exps.csv", string.Format("{0},{1}", time, e.Exception.Message));
    File.AppendAllText(@"C:\sts.txt", time + ":\n" + e.Exception.StackTrace + "\n");
    e.Handled = false; //we want the user to experience the crash
}

But the dispatcher not working and not execute when the application throw exception.

Noam M
  • 3,156
  • 5
  • 26
  • 41
  • 1
    Why can't you surround your dangerous code with try catch? – FCin Nov 08 '16 at 06:19
  • 1
    `DispatcherUnhandledException` will catch the unhandled exception only from the UIThread. Unhandled exception on other threads won't be caught here. – Gopichandar Nov 08 '16 at 06:22
  • 1
    It is critical to keep the app running all the time. That is why I need to handled unpredicted exception @FCin – Cactus Jack Nov 08 '16 at 06:24
  • 1
    @Gopichandar I tried also to throw exception on the main ui, it was not handled by the DispatcherUnhandledException – Cactus Jack Nov 08 '16 at 06:25

0 Answers0