Unhandled AppDomain Exceptions

There are many topics that cover this issue. But nevertheless, I have a problem.

I load the assembly into a new AppDomain as follows:

 public void Run() { //There the problem. //As Panos Rontogiannis mentioned the thread is created in default AppDomain new Thread(RunApp).Start(); } private void RunApp() try { AppDomain.CreateDomain("domain name").ExecuteAssembly("path to assembly"); } catch (Exception _e) { MessageBox.Show("Unhandled Exception.\n" + _e); } } 

In the main method of the loaded assembly, I signed my handler for the UnhandledException event:

 AppDomain.CurrentDomain.UnhandledException += handleException; 

The handler itself:

 public static void handleException(object a_s, UnhandledExceptionEventArgs a_args) { var _e = (Exception)a_args.ExceptionObject; //Static loger class method Loger.WriteError(_e.GetType().ToString(), _e.Message, "default solution"); } 

But wherever an exception is thrown in a loaded assembly, the handler is not involved. I have an exception only by default AppDomain (first try{} catch{} ).

+4
source share
6 answers

Most likely, the reason you cannot handle the exception in the new AppDomain is because it is not selected from the thread created in this AppDomain . From the documentation for AppDomain.UnhandledException it is not very straightforward to see this. The interesting part is as follows:

An exception is not processed only if the entire stack for the thread has been unwound without searching for a suitable exception handler, so the first place that can be raised is in the application domain where the thread originated.

Now, if the thread that runs the code that generates is created mainly by AppDomain (for example, in the main thread of the console application), then you should add a handler in the main AppDomain . Please note that if the type of the eliminated throw is not loaded into the main AppDomain, the .NET download collector will try to load it from the base directory of applications and detection zones. If they do not match the child of the AppDomain , the assembly will not be allowed and an (other) exception will be thrown.

+3
source

There are many reasons why this can happen. The event documentation at http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx details this complexity. If nothing seems applicable, could you post the repro code?

+1
source

My assumption is that the handler is not called because the exception is being handled. those. top attempt {} catch {}.

0
source

I did not understand why the handler is not called.

I ended up using FirstChanceException instead of UnhandledException in the main method of the loaded assembly. Like this:

 AppDomain.CurrentDomain.FirstChanceException += (obj, e) => Loger.WriteError( e.Exception.ToString(), e.Exception.Message, "default solution" ); 
0
source

This is a late answer, but it seems to work fine if you ask me (VS2012 / .NET 4.5), the exception handler must be registered before calling ExecuteAssembly, of course: (I have a child process that causes Access Violation by writing to null ref (unsafe code) is just for forced failure and throws a HandleException below:

 public static void HandleException(object a_s, UnhandledExceptionEventArgs a_args) { var _e = (Exception)a_args.ExceptionObject; Console.WriteLine(_e.GetType().ToString(), _e.Message, "default solution"); } public void StarProcessWithinAppDomain(string fileName) { try { // New appdoamin / check exception isolation level AppDomain sandBox = AppDomain.CreateDomain("sandBox"); try { AppDomain.CurrentDomain.UnhandledException += HandleException; sandBox.ExecuteAssembly(fileName); } catch (Exception ex) { Console.WriteLine("An error occurred (inner) within AppDomain, executing \"{0}\":" + "\n" + ex.Message, fileName); } finally { AppDomain.Unload(sandBox); } } catch (Exception ex) { Console.WriteLine("An error occurred within AppDomain, executing \"{0}\":" + "\n" + ex.Message, fileName); } } 
0
source
  • FirstChanceException fire.
  • Any catch blocks are catch .
  • If the block does not have a catch or throw block, then an UnhandledException fires

Your catch ensures that UnhandledException does not fire.

0
source

All Articles