Unable to catch exception from Activator.CreateInstance.

Posted by Patrik Hägne on Stack Overflow See other posts from Stack Overflow or by Patrik Hägne
Published on 2010-05-10T14:14:17Z Indexed on 2010/05/10 15:44 UTC
Read the original article Hit count: 346

Filed under:
|
|
|

OK, I admit it this code will just look weird to you, and that's because it is weird. This is just code to reproduce the behavior, not code I want to use.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Activator.CreateInstance(typeof(Func<int>), new object[] { new object(), IntPtr.Zero });
        }
        catch
        {
            Console.WriteLine("This won't print!");
        }

        Console.Write("Actually this will not print either!");
        Console.ReadLine();
    }
}

No matter what exception type I try to catch (the actual exception thrown is an ArgumentException as far as I can tell) the code inside the catch block will not execute. Actually execution will just stop at the Activator.CreateInstance-line.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#