I am developing a lightweight MVVM WPF framework and would like to be able to catch unhandled exceptions and recover perfectly from them.
Ignoring at the moment all the good arguments in favor of this, I come across the following situation:
If I register a handler for AppDomain.CurrentDomain.UnhandledException in the OnStartup method for App.xaml.cs, as follows ...
App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler);
base.OnStartup(e);
}
void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ea)
{
Exception e = (Exception)ea.ExceptionObject;
}
and then throw an exception in one of my virtual machines, the handler is called as expected.
So far so good, except for the fact that I cannot restore this approach, all I can do is to register an exception and then allow the CLR to terminate the application.
, , - VM. ( ).
, , AppDomain.CurrentDomain.UnhandledException , :
protected override void OnStartup(StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(this.AppDomainUnhandledExceptionHandler);
this.DispatcherUnhandledException +=
new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledExceptionHandler);
base.OnStartup(e);
}
void AppDomainUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ea)
{
Exception e = (Exception)ea.ExceptionObject;
}
void DispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs args)
{
args.Handled = true;
}
, , . .DispatcherUncepted, . , DispatcherUnhandledExceptionHandler - AppDomain.CurrentDomain.UnhandledException.
- ?
, , .