Wpf Global Exception Handler

WPF global exception handler

You can handle the AppDomain.UnhandledException event

EDIT: actually, this event is probably more adequate: Application.DispatcherUnhandledException

WPF MVVM Capture Global Exception Handler to Context

Why do you have to handle the exceptions in the apps code behind?

You can do that in your main window viewmodel. I am doing this and it is working fine.

if (Application.Current != null)
{
Application.Current.DispatcherUnhandledException += (s, a) =>
{
Reporter.AddLog(a.Exception);

DisplayAppropriateNotification(a);

a.Handled = true;
};
}

This is what I am doing in the constructor of my MainWindowViewmodel.

I guess you will have the context created in the viewmodel.

Globally catch exceptions in a WPF application?

Use the Application.DispatcherUnhandledException Event. See this question for a summary (see Drew Noakes' answer).

Be aware that there'll be still exceptions which preclude a successful resuming of your application, like after a stack overflow, exhausted memory, or lost network connectivity while you're trying to save to the database.

WPF how to handle Exceptions and continue

Frankly, your entire design is a bad idea, in my opinion. It appears to me that you want to be able to "handle" these exceptions by logging them and letting your program continue executing. But that's an incredibly dangerous approach and definitely not recommended. You should only catch and handle exceptions for which you know in advance what the exception is and what the safe way to handle it is.

To do otherwise is to risk leaving your program in an unknown state, leading to anything from (at best) buggy behavior to (at worst) permanently corrupting the state of user's important data.

See also Should you catch all exceptions?

But, assuming you're going to do this anyway, you're not going to be able to use the LogUnhandledException() method to set the Handled property in the event args, because each of those events is different. Only the DispatcherUnhandledException event even has a Handled property to set. The UnobservedTaskException has an Observed property which you can set, and the AppDomain.UnhandledException event doesn't even have an analogous property.

However, you can of course specialize each handler to do that. For example:

protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += (s, ex) =>
LogUnhandledException((Exception)ex.ExceptionObject,
"AppDomain.CurrentDomain.UnhandledException");

DispatcherUnhandledException += (s, ex) =>
{
LogUnhandledException(ex.Exception,
"Application.Current.DispatcherUnhandledException");
ex.Handled = true;
};

TaskScheduler.UnobservedTaskException += (s, ex) =>
{
LogUnhandledException(ex.Exception,
"TaskScheduler.UnobservedTaskException");
ex.SetObserved();
};
}

Global exception handling in MVVM

After a long battle finally I have found a very easily way to implement handling exceptions inside of ViewModel. While creating a BindingListener that inherits from DefaultTraceListener is certainly a great way to find your binding errors during the debug mode, this will not catch exceptions that have occurred inside a ViewModel when running solution is standard mode. But AppDomain.CurrentDomain.FirstChanceException will.

App.xaml.cs:

AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);


private void CurrentDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() => MessageBox.Show("Error Occurred \n\r" + e.Exception.Message + "\n\r" + e.Exception.StackTrace, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error)));
}

Global exception handler for MVVM Light application

I think I figure this out now.

The problem is due to the fact that WPF suppresses exceptions thrown in a view's databinding, and because my view model is databound to the view's DataContext (through a property in my ViewModelLocator utilizing unity dependency injector) any exceptions in the construction of the view model will be swallowed.

See this SO question for more info.

So I suppose I just have to make sure that nothing important for the application's ability to function correctly should happen in the constructor.

Global handling exception in WPF app with Caliburn.Micro

Caliburn.Micro has built in support for hooking unhandled exceptions. The Bootstrapper class (which every Caliburn project requires) sets this up for you and provides the virtual OnUnhandledException method.

In your custom BootStrapper you must override OnUnhandledException to perform any custom actions for unhandled exceptions in your app. Note that you will most likely have to marshal actions such as displaying a message box to the UI thread (Caliburn enables this easily via Execute.OnUIThread).

You may also have an issue in the way your service moves exceptions to the client, but without any details of how the service is implemented/hosted/etc I cannot help. Are you using WCF to do SOAP? Are you using FaultContracts?

Global Error Handling in Task.Run

There is an TaskScheduler.UnobservedTaskException event that you can use

TaskScheduler.UnobservedTaskException += (s, e) => {
e.Exception //The Exception that went unobserved.
e.SetObserved(); //Marks the Exception as "observed," thus preventing it from triggering exception escalation policy which, by default, terminates the process.
};

Occurs when a faulted task's unobserved exception is about to trigger exception escalation policy, which, by default, would terminate the process.

This application domain-wide event provides a mechanism to prevent exception escalation policy (which, by default, terminates the process) from triggering


Problems with global exception handlers for unhandled exceptions in multithreaded WPF

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 += ...
}


Related Topics



Leave a reply



Submit