views:

268

answers:

1

Here's my context:

  • I am writing a WPF add-in for an application.
  • This Application's main thread is unmanaged.
  • I want to add a global exception handling system for this add-in to handle any unhandled exceptions.

Here's what I've tried but not working:

  • I cannot add a try-catch block to my Application.Run() code line. Because I am an add-in, that code fragment is in the application.
  • System.Windows.Forms.Application.ThreadException is not working too. There might not be an WinForm Application exists. (WPF hosting in unmanaged code.)
  • AppDomain.CurrentDomain.UnhandledException is not working too. Because maybe it's handled by the Application itself. It just doesn't enter my code.

So, any ideas for this situation?

A: 

You shouldn't touch global exception handling even if you could, you are writing an add-in and hooking into global exception handling will change not just the behavior of your add-in but also the main application and every other add-in in the system - doing something that may introduce bugs into other add-ins is not being a good neighbor :-)

Now, what you can do it wrap every externally callable method in your add-in with a try-catch block, this is obviously unmaintainable but an AOP system like PostSharp can easily add those for you while keeping the handling code in a nice central location.

Nir
Thank you for your answer. You are right, I shouldn't add a global exception handling for this application. But is there any way to add a "global" exception handling system to only my add-in region - only the exceptions which thrown by my add-in would go to this system.Add try-catch blocks only to my entries are not enough because I have a bunch of behaviors in my add-in's GUI..Thank you very much for your point again.
redjackwong
You can use PostSharp to automatically add try-catch like behavior to all your code - even code in properties, converters, behaviors and user/custom controls - so that should cover most cases, I don't have a good solution for exceptions thrown by someone else's code inside your GUI
Nir