views:

103

answers:

2

I am working on my MCTS and currently studying the AppDomain functionality. But I am running into something unclear. AppDomain should be capturing Exception and allow the domain to safely unload. (With the possible exception of the StackOverflowException as suggested elsewhere)

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
domain.UnhandledException += new UnhandledExceptionEventHandler(domain_UnhandledException);
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, setup);

When I decide to create an instance of the example class in an assembly I created for this purpose I should be getting a safe, restricted Domain which will capture errors that occur and can be safely unloaded. At least this is how I understand it from my study book.

var type = (IDoSomeWork) domain.CreateInstanceAndUnwrap("Library1", "Library1.Class1");
type.Run();

This throws an exception however on type.Run() (since I made it that way). But shouldn't the AppDomain capture it safely? Isn't that why we have an AppDomain?

UPDATE:

As requested, I have included the definition of the Library1.Class1. Also, for clarity, the UnhandledExceptionEventHandler has no influence on capturing the exception and isn't relevant to the question.

[Serializable]
public class Class1 : MarshalByRefObject, IDoSomeWork
{
    public void Run()
    {
        Debug.WriteLine(AppDomain.CurrentDomain.FriendlyName);
        throw new ApplicationException(String.Format("{0}", this.ToString()));
    }
}

I have verified that it runs in MyDomain.

+1  A: 

The UnhandledException event does not capture the exceptions in the traditional sense, like a try-catch block (to the best of my knowledge), it is merely an information point, allowing you to perform logging and such tasks.

Quoted from the MSDN documentation:

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.

So the exception will still bubble up to the system default exception handler.

Fredrik Mörk
Why wouldn't the exception stay contained within the ApplicationDomain though? I'm not sure this completely answered the question.
Taylor Leese
@Taylor: because the event is not designed to work like that. The documentation clearly states that the event handler allows the application to collect information *before the system default handler reports the exception to the user and terminates the application*. It is not the purpose of the event to work as a global catch block.
Fredrik Mörk
I think the main point of the question is about why the default AppDomain is getting the exception. For example, what would happen if there was was no UnhandledExceptionEventHandler for the custom (non-default) domain?
Taylor Leese
@Taylor: re-reading the question and I my answer I see your confusion, and must say that I start to share it. I'll let the answer be for now (short of time with other tasks), but will come back after some experimentation later, unless someone else already shed some light on the issue until then.
Fredrik Mörk
I asked a question regarding AppDomains recently and there seemed to be some disagreement related to this topic as well (comments on Reed Copsey's answer). See here http://stackoverflow.com/questions/1639244/common-uses-and-best-practices-for-application-domains-in-net
Taylor Leese
A: 
 AddHandler Application.ThreadException, AddressOf MyExceptionHandler.HandleException

Adds a top-level exception handler but it might not do what you want in this case.

Joshua