I'm not intrigued by the BadImageFormatException that is thrown when I declare a variable of a Visual C++ type that is defined inside a referenced Visual C++ assembly as much as I am intrigued by the fact that the exception is not caught by the catch clause of the try-catch statement immediately surrounding the variable declaration, but by the catch clause of the try-catch statement surrounding the method call to the method that declares the variable.
public static void method()
{
try
{
Some_Visual_Cpp_Type o;
}
catch (Exception)
{
Console.WriteLine("caught inside the method");//apparently not called
}
}
public static void Main()
{
try
{
method();
}
catch (BadImageFormatException)
{
Console.WriteLine("caught outside the method");//prints "caught outside the method"
}
}
Can someone please explain this behavior?