1
votes

My application is an assembly loaded dynamically by a parent WPF application.I handle the AppDomain.UnhandledException from my component to handled the exception thrown from any background threads.

However it seems that since the parent application has also registered for the same event the unhandled exception is propagated to the parent application as well.

Is there any to prevent the unhandled exceptions not to propagate to my parent application.

I will not be able to change the parent application behavior as this might affect other components running inside the parent application.

Thanks

2
Are you catching using System.Exception?om471987
Linquacious,AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) { if (e.IsTerminating) { ExceptionProcessor.HandleError(e.ExceptionObject as Exception, "An unknown error occured.The Component will be closed", true, true); } else { ExceptionProcessor.HandleError(e.ExceptionObject as Exception, "An unknown error occured.", true); } }Vasudevan Kannan
Why don't you create different handler within you code so the handler by parent application will never be called!om471987
Linquacious,The code i posted in the previous comment is in my component.The parent application has a simillar event handler as well.When an exception occurs both the handlers are inovked.Vasudevan Kannan

2 Answers

0
votes

Give an explicit name of the eventhandler like this

You namespace is MyCompany.MyProduct.Service You parent's namespace is MyCompany.MyProduct.Parent

Instead of using

AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException(Myhandler);

use following

AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException(MyCompany.MyProduct.Service.MyHandler);

I hope the problem will be solved.

0
votes

Just reading the documentation from MSDN,

If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in. If the thread started in an application domain that has an event handler for UnhandledException, the event is raised in that application domain. If that application domain is not the default application domain, and there is also an event handler in the default application domain, the event is raised in both application domains.

For example, suppose a thread starts in application domain "AD1", calls a method in application domain "AD2", and from there calls a method in application domain "AD3", where it throws an exception. The first application domain in which the UnhandledException event can be raised is "AD1". If that application domain is not the default application domain, the event can also be raised in the default application domain.

So the solution is create a seperate domain for my component and create the event handler for my domain

  AppDomain domain = AppDomain.CreateDomain("MyDomain");

  domain.UnhandledException += MyDomainUnhandledException
  static void CurrentDomainUnhandledException(objectsender,UnhandledExceptionEventArgs e) 
  {
   //Exception handling logic for my domain
  }

This will make sure that the exceptions do not propogate to the parent application