# Friday, January 14, 2005

I'm currently working on some samples that showcase how best to use shadow copying, set private bin paths and load/use add-ins. I'll start posting these when I get the samples done and come up with a good story around them. My next post, which is almost done, is on Assembly.LoadFrom() and Reflection.

Anyway, I'm curious about what aspects of the CLR Loader and AppDomains that folks are most interested in/confused about. I've seen a bunch of posts on the microsoft.public.dotnet.framework.clr newsgroup that I'm using as a good sample of areas to post on -- hence my first post.

Friday, January 14, 2005 6:39:59 AM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [12]  | 
# Wednesday, December 15, 2004

I was reading the CLR newsgroup and noticed the following post about unhandled exceptions. I wasn't quite sure exactly how this worked, so I wrote a quick app that caught exceptions from other domains in the defaul domain. This behaviour was what the poster was looking for.

You can download the code [Everett | Whidbey ] to see how it works.

The basic idea is that you register for the AppDomain.UnhandledException event. This event will be fired for any exception that is not handled in any domain in the process. You cannot swallow the exception at this point, so your app will die no matter what. You can, however, do any cleanup that is required or put up some kind of user notification. In the case below, you can see that I tell the user that an unhandled exception occured. I'm sure that this is going to be quite useful to them ;)

Here is the bulk of the code:

    class Program
    {
        static void Main(string[] args)
        {

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            AppDomain domain2 = AppDomain.CreateDomain("domain2");
            domain2.CreateInstance("DomainLib", "DomainLib.ThisClassThrows");

        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            Console.WriteLine("Unhandled exception!!");
            Console.WriteLine(ex.InnerException.Message);
        }
    }

Another option, which is a good idea, is to use try/catch blocks in your code around calls to other domains. That way you can catch the exceptions that come across the domain boundary as opposed to just settle for process termination.

Wednesday, December 15, 2004 6:45:03 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [11]  |