Exception exception from AppDomain.AssemblyLoad

Can someone explain to me why I cannot throw an exception from within the AppDomain.Assembly loading event? For instance:

class Program
{
    static Program()
    {
        AppDomain.CurrentDomain.UnhandledException += (s, a) =>
        {
            Console.WriteLine("Caught exception!");
        };

        AppDomain.CurrentDomain.AssemblyLoad += (s, a) =>
        {
            Console.WriteLine(string.Format("Assembly {0} loaded", a.LoadedAssembly.FullName));

            throw new Exception();

            Console.WriteLine("Should never get here...");
        };
    }

    static void Main(string[] args)
    {
        Console.WriteLine(new ClassLibrary1.Class1().TestString());
        Console.WriteLine();
        Console.WriteLine("Done...");
        Console.ReadLine();
    }
}

When I do this, the output is as follows:

Assembly ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null loaded
TestString
Done...

Can someone explain this behavior to me? Thank.

EDIT To clarify a couple of things:

  • The assembly load event runs fine when I expect it to fire. But my exception never throws

  • This is a distilled example taken from a larger application. I want to check the assembly after loading it, and if I don’t like it, I want to work quickly ... But my exception doesn’t happen "

+5
source share
4

, ? , , " ...". , , , -, .

, , - . , , AppDomain.AssemblyLoad, .

+1

- , JIT-. Main(), . ClassLibrary1.Class1(), . , . , :

using System.Runtime.CompilerServices;
...
    static void Main(string[] args) {
        Test();
    }
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Test() {
        Console.WriteLine(new ClassLibrary1.Class1().TestString());
        Console.WriteLine();
        Console.WriteLine("Done...");
        Console.ReadLine();
    }

AssemblyLoad , ClassLibrary1 .

+1

. .Net , (Main()). , Debug- > Exceptions "Throw" Runtime ", .

+1

, , asynccallback. , Application.ThreadException + = System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

,

0

All Articles