FrameWork-level exception elimination in WPF

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;
  // log exception
}

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;
  // log exception
}

void DispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs args)
{
  args.Handled = true;
  // implement recovery
}

, , . .DispatcherUncepted, . , DispatcherUnhandledExceptionHandler - AppDomain.CurrentDomain.UnhandledException.

- ?

, , .

+3
2

, VS , , , ( , , VS ). , Visual Studio, Debug->Exceptions.

, , .

, DispatcherUnhandledException, , .

+5

:

...

App.xaml.cs:

protected override void OnStartup(StartupEventArgs e)
{
  Application.Current.DispatcherUnhandledException +=
    new DispatcherUnhandledExceptionEventHandler(DispatcherUnhandledExceptionHandler);

  base.OnStartup(e);
}

void DispatcherUnhandledExceptionHandler(object sender, DispatcherUnhandledExceptionEventArgs args)
{
  args.Handled = true;
  // implement recovery
  // execution will now continue...
}

[: , IDE (Visual Studio) IDE. , . .]

, , VisualStudio, VS, DispatcherUnhandledExceptionHandler F5/continue, .

, Windows, , , - .

+2

Source: https://habr.com/ru/post/1784079/


All Articles