0

When you need handle all exceptions in your WPF application you can use:

Application.DispatcherUnhandledException event

Like explained here and here.

My problem is:

I create a WPF Custom Control Library, so I don't have a app.xaml file. I can't define a Application.DispatcherUnhandlerException.

And even more so, my library will be used to a non .net based app. So I can't define Application.DispatcherUnhandlerException in the main application, because there isn't one.

Is there a way to make this in a dll level?

Community
  • 1
  • 1
Jonny Piazzi
  • 3,684
  • 4
  • 34
  • 81

2 Answers2

3

IMHO you don't want this. Regardless if it's possible or not. You don't want exceptions to die quietly in libs. Clients using your libs should decide what to do with your exceptions. Maybe they want to log it. Send it to a web api. Store it in the db. Whatever. You should only handle specific exceptions in your libs witch your lib could actually deal with. The rest you should bubble up.

Danny van der Kraan
  • 5,344
  • 6
  • 31
  • 41
  • I agree with you, is not good kill the exceptions inside the library. But i'm not interested in kill the exceptions. I'm interested in handler them, it's possible to handler a exception and even so let it continue be throw above. Another point is that, i'm not distributing my library, i'm using it to a very specific purpose. This library will be a part of a system, but the system it not in .net. So I need to put the log of exceptions inside my library. – Jonny Piazzi Apr 26 '16 at 13:23
0

I do not think you need an app.xaml to register for those events.

If you do want to do this, add this code in a static initializer in one of your classes that will for sure be used by anyone using the control library:

if (AppDomain.CurrentDomain != null) {
   AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

if (Dispatcher.CurrentDispatcher != null) {
   Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
}

 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
   // Do something with the exception in e.ExceptionObject
 }

 static void CurrentDispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
   // Do something with the exception in e.Exception
 }
user469104
  • 1,206
  • 12
  • 15