try catch 块 在catch块或者try块 return 掉 finally会执行吗?

1.在catch块 return 掉 finally会执行吗? 答案:会

代码:

点击查看代码
try
{
    int s = 1;
    Console.WriteLine(1);
    var a = 5 / (1 - s);//除以 0  捕获异常
}
catch (Exception)
{
    Console.WriteLine(2);
    return;
}
finally
{
    Console.WriteLine(3);
}
Console.ReadKey();

结果输出:
image

2.在try块 return 掉 finally会执行吗?答案:会

代码:

点击查看代码
try
{
    int s = 1;
    Console.WriteLine(1);
    return;
    ////var a = 5 / (1 - s);//除以 0  捕获异常
}
catch (Exception)
{
    Console.WriteLine(2);
    //return;
}
finally
{
    Console.WriteLine(3);
}
Console.ReadKey();

结果输出:
image

posted on 2023-11-23 15:59  RookieBoy666  阅读(5)  评论(0编辑  收藏  举报