在.NET中,为什么可以将其抛出的方法中捕获到badimageformatexception中?
当我声明视觉C ++类型的变量时,它在引用的视觉C ++汇编内定义的视觉C ++类型时所抛出的BadImageFormateCeption并不吸引我很感兴趣,就像我对异常未被捕获条款所抓住的事实所吸引围绕变量声明的try-catch语句,但通过围绕该方法调用的try-catch语句的捕获子句,呼叫变量的方法。
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"
}
}
有人可以解释这种行为吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当方法被插在捕获块之前时,将抛出此例外。
This exception is thrown when the method is JITted, before its catch block exists.
在@slaks上,在这种特殊情况下回答了
导致
BadImageFormateXception
的项目确实是该方法内部的,但这并不一定是正确的。如果some_visual_cpp_type
值被声明为参数怎么办?然后,捕获块将可以访问导致BadImageFormateXception
的值。当确定可执行文件中的特定位置是不可敲打时,
badImageFormateXception
会提高。而不是试图在方法中选择一个安全点以抛出异常,而是完全放弃了该方法。试图将方法的好部分与不良方法分开,几乎没有什么可获得的。对于JIT和开发人员来说,完全声明该方法并从那里继续前进要简单得多。另请注意,不能保证
BadImageFormateXception
在调用方法中甚至可以捕获。如果JIT决定内联方法
在main
的内部,那么当调用main
时,将抛出异常Building on @Slaks answer
In this particular case the item causing the
BadImageFormatException
is indeed inside the method but that doesn't have to be true. What if theSome_Visual_Cpp_Type
value was declared as a parameter instead? Then the catch block would have access to the value which caused theBadImageFormatException
in the first place.The
BadImageFormatException
is raised when a particular place in the executable is determined to be un-runnable. Instead of trying to pick a safe point in the method to throw the exception the JIT just abandons the method entirely. There is little to be gained by trying to parse apart the good parts of the method from the bad ones. It's much simpler to the JIT and developer to just declare the method altogether bad and move on from there.Also note that it is not guaranteed that the
BadImageFormatException
is even catchable in the calling method. If the JIT decides to inlinemethod
inside ofMain
then the exception will be thrown whenMain
is called and hence uncatchable inside of there