gpt4 book ai didi

c# - 在 .NET 中,为什么不能在抛出 BadImageFormatException 的方法中捕获它?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:42 25 4
gpt4 key购买 nike

当我声明一个在引用的 Visual C++ 程序集中定义的 Visual C++ 类型的变量时抛出的 BadImageFormatException 我并不感兴趣,我对异常未被捕获这一事实很感兴趣try-catch 语句的 catch 子句紧邻变量声明,但由 try-catch 语句的 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"
}
}

有人可以解释一下这种行为吗?

最佳答案

当方法被 JIT 时,在它的 catch block 存在之前抛出这个异常。

关于c# - 在 .NET 中,为什么不能在抛出 BadImageFormatException 的方法中捕获它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20412188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com