Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling critical exceptions in different appdomains

Tags:

c#

Let's asume the folowing bit of code, which allows you to call a class in a different AppDomain and handle almost any exception:

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace MyAppDomain
{
  class Program
  {
    static void Main(string[] args)
    {
      AppDomain myDomain = null;
      try
      {
        myDomain = AppDomain.CreateDomain("Remote Domain");
        myDomain.UnhandledException += new UnhandledExceptionEventHandler(myDomain_UnhandledException);
        Worker remoteWorker = (Worker)myDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Worker).FullName);
        remoteWorker.VeryBadMethod();
      }
      catch(Exception ex)
      {
        myDomain_UnhandledException(myDomain, new UnhandledExceptionEventArgs(ex, false));
      }
      finally
      {
        if (myDomain != null)
          AppDomain.Unload(myDomain);
      }

      Console.ReadLine();
    }

    static void myDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
      Exception ex = e.ExceptionObject as Exception;
      if (ex != null)
        Console.WriteLine(ex.Message);
      else
        Console.WriteLine("A unknown exception was thrown");
    }
  }

  public class Worker : MarshalByRefObject
  {
    public Worker()
    {

    }

    public string DomainName
    {
      get
      {
        return AppDomain.CurrentDomain.FriendlyName;
      }
    }

    public void VeryBadMethod()
    {
      // Autch!
      throw new InvalidOperationException();
    }

  }
}

Now the problem is, allmost any exception can be handled, not every exception. A StackOverflowException for example will still crash the process. Is there a way to detect critical exceptions in different appdomains and handle these by unloading the AppDomain, but still allow other AppDomains to continue?

like image 606
Jeroen Landheer Avatar asked Jun 12 '09 15:06

Jeroen Landheer


People also ask

How to handle exceptions Globally in C#?

An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.

What happens if catch block throws exception C#?

The C# try and catch keywords are used to define a try catch block. A try catch block is placed around code that could throw an exception. If an exception is thrown, this try catch block will handle the exception to ensure that the application does not cause an unhandled exception, user error, or crash the application.

How does CLR handle exception?

Exception, the CLR execution engine can raise exceptions, and unmanaged code can raise exceptions as well. Exceptions raised on a thread of execution follow the thread through native and managed code, across AppDomains, and, if not handled by the program, are treated as unhandled exceptions by the operating system.

How to throw exception in try catch C#?

try { if (isFileDownloaded) doSomeThings(); else throw new ApplicationException("Something expectedly unexpected happened."); } catch(ApplicationException e) { // log application exception here... } catch(Exception e) { // log all other exceptions here... } finally { // release resources... }


1 Answers

Unfortunately a StackOverflowException cannot be caught.

See: http://msdn.microsoft.com/en-us/library/system.stackoverflowexception.aspx

... Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default. ...

Update:

After further investigation in my old issues I found this old thread: http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=36073

like image 93
Dr Spack Avatar answered Nov 01 '22 02:11

Dr Spack