如果我/终于在C#中恢复出来一试确实在代码终于始终运行? [英] If i return out of a try/finally in C# does the code in the finally always run?

查看:202
本文介绍了如果我/终于在C#中恢复出来一试确实在代码终于始终运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎像它根据初步的测试结果,但是我想知道它是否保证返回,或者在某些情况下,它不能返回...?这是我的应用程序的关键,但我还没有找到一个用例尚未它难道不回来,但我还想坐上subjet专业知识。

It seems like it does as per some initial testing, but what id like to know if it is guaranteed to return or if in some cases it can not return ...? This is critical for my application but i havent find a usecase yet where it wouldnt return but id like to get expertise on the subjet.

感谢您的帮助!

推荐答案

在finally块内容都将始终无论try或catch块里面发生了什么执行。如果返回形成方法与否并不重要。

Anything in a finally block will always be executed regardless of what happens inside the try or catch blocks. It doesn't matter if you return form the method or not.

唯一的例外是,如果在finally块中的代码抛出一个异常,那么它会停止执行类似代码的任何其它块。

The only exception to this is if the code in the finally block throws an exception, then it will stop executing like any other block of code.

在关于跳转,答案是肯定的还是。考虑下面的代码:

In regards to goto, the answer is still yes. Consider the following code:

try
{
    Console.WriteLine("Inside the Try");
    goto MyLabel;
}
finally
{
    Console.WriteLine("Inside the Finally");
}

MyLabel:
    Console.WriteLine("After the Label");



产生的输出是这样的:

The output produced is this:

在try

在最后

标签之后

这篇关于如果我/终于在C#中恢复出来一试确实在代码终于始终运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆