在.NET中,为什么可以将其抛出的方法中捕获到badimageformatexception中?

发布于 2025-01-20 22:29:32 字数 701 浏览 2 评论 0原文

当我声明视觉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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯下孤影 2025-01-27 22:29:32

当方法被插在捕获块之前时,将抛出此例外。

This exception is thrown when the method is JITted, before its catch block exists.

感性 2025-01-27 22:29:32

在@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 the Some_Visual_Cpp_Type value was declared as a parameter instead? Then the catch block would have access to the value which caused the BadImageFormatException 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 inline method inside of Main then the exception will be thrown when Main is called and hence uncatchable inside of there

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文