Gerard
Gerard

Reputation: 13397

How to handle an unexpected exception in usercontrol

Suppose a wpf App consists essentially of several tabitems each with a rich usercontrol. All usercontrols are in the same UI thread and an unexpected and unhandeld error will crash the App.

Is there a way to avoid this and limit the crash to the particular usercontrol where it occured? And the user can continue on the other tabs.

One way would be to use a separate window on a different thread instead of a tabitem.

Upvotes: 2

Views: 1422

Answers (1)

Sheridan
Sheridan

Reputation: 69959

You can handle the AppDomain.UnhandledException event, although usually at this stage, something has gone badly wrong and this event is normally handled in order to log error details and/or close the program gracefully:

AppDomain.CurrentDomain.UnhandledException += MainWindow_UnhandledException;

...

public void MainWindow_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // An unhandled Exception has been thrown
}

It will catch all Exceptions that you have not handled manually. However, there is often only minimal information apart from the StackTrace as to exactly where the Exception was thrown. Give it a go and see if it helps you.

Upvotes: 2

Related Questions