11

Edit

The answers to this question where helpful thanks I appreciate the help :) but I ended up using: http://code.msdn.microsoft.com/windowsdesktop/Handling-Unhandled-47492d0b#content


Original question:

I want to show a error message when my application crashes.

Currently I have:

App.xaml:

<Application x:Class="WpfApplication5.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      DispatcherUnhandledException="App_DispatcherUnhandledException"  // <----------------
      StartupUri="MainWindow.xaml">
    <Application.Resources>             
    </Application.Resources>
</Application>

Code-behind of App.xaml:

namespace WpfApplication5
{        
    public partial class App : Application
    {
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Caught Unhandled Exception!");
        }
    }
}

That solution works great when the error occurs on the main thread. Now my problem is how will I be able to catch errors that happen on a different thread also?

In other words: when I press this button I am able to catch the exception: (App_DispatcherUnhandledException gets called!)

    private void button1_Click(object sender, RoutedEventArgs e)
    {            
        int.Parse("lkjdsf");
    }

But I am not able to catch this exception:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Task.Factory.StartNew(() =>
        {
            int.Parse("lkjdsf");
        });
    }

How will I be able to catch all exceptions regardless if they happen on the main thread or not?

Tilak
  • 30,108
  • 19
  • 83
  • 131
Tono Nam
  • 34,064
  • 78
  • 298
  • 470

2 Answers2

11

You can use AppDomain.UnhandledExceptionHandler to handle uncaught exception.

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • `System.AppDomain.UnhandledException` gives me an error weird why? I will keep trying. – Tono Nam Mar 20 '13 at 15:02
  • 1
    got it to work I had to do: `System.AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);` – Tono Nam Mar 20 '13 at 15:04
  • @TonoNam Does your app gets closed after you catch `System.AppDomain.UnhandledException` – Muhammad Arslan Jamshaid Dec 08 '15 at 17:34
  • Nope application still runs. I was just wanted to know whenever exceptions occurred on different threads. – Tono Nam Feb 15 '16 at 16:52
  • 2
    This does not capture unhandled exceptions from other threads. `Task.Factory.StartNew(() => throw new Exception("Hey this is an exception"));` Would not be captured by this event handler. – Xav Nov 29 '17 at 17:06
2

Use AppDomain.UnhandledException event to catch this exception. than you need to use the dispatcher to be able to show this exception in a messageBox (this is because only ui thread can show messages)

omer schleifer
  • 3,897
  • 5
  • 31
  • 42