为什么抛出异常后最终块可能无法执行? [英] Why finally block may not execute when exception is thrown?

查看:94
本文介绍了为什么抛出异常后最终块可能无法执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长时间以来,我认为它可以释放 finally 块中的所有资源,并且我认为如果<$ c $中发生异常c> try 块,然后仍将在 finally 块中释放资源。

For a long time I thought that it allows me to free up all the resources in the finally block and I thought that if an exception happens in the try block, then the resources will still be free up in the finally block. But that seems not to be the case.

我有以下代码:

using System;

public sealed class Program
{

    public static void Main()
    {
        try {
            int zero = 0;
            int i = 1/zero;
        } finally {
            Console.WriteLine("divide by zero"); //the line is never called
        }
    }
}

我从未到达打印到控制台的行。这意味着在这种情况下,我将无法释放 finally 块中的资源,除非抛出了 try 块。

I never reach the line which prints to the console. That means that I will not be able to free up resource in finally block in this case on the exception being thrown inside the try block.

所以,我相信有两件事:我错过了一些东西或试试 c 最终组合在C#中没有用例。第二条语句很有意义,因为我将获得与上面的代码和下面的代码相同的功能:

So, I believe there are two things: either I am missing something or the try + finally combination has no use cases in the C#. The second statement makes sense, because I will get the same functionality as is produced by the above code with the code below:

using System;

public sealed class Program
{

    public static void Main()
    {
            int zero = 0;
            int i = 1/zero;

            Console.WriteLine("divide by zero"); //the line is never called
    }
}

我可能在这里错过了一些东西。因此,请问有人可以确认组合是无用的还是证明它不是呢?

But I am afraid that I might be missing something here. So, could someone confirm that the combination is useless or prove that it is not, please?

更新

能够在小提琴中最终调用 的注释之后,我再次在VS Code中进行了检查,但仍然看不到任何输出。

After the comment which is able to call the finally block in its fiddle, I checked once more in the VS Code, and still I see no output.

推荐答案

您的假设是错误的(有时) https://dotnetfiddle.net/hjqmOS

You're assumptions are incorrect (sometimes) https://dotnetfiddle.net/hjqmOS

尝试最终(C#参考)


通过使用finally块,您可以清除任何$的资源。 b $ b在try块中分配,您可以运行代码即使try块中发生了异常
。通常,当控制离开try语句时,finally块
的语句运行。正常执行,执行中断,执行
,继续执行goto或return语句或从try语句中传播异常
都会导致
的控制权转移

By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

在某些情况下,虽然它没有运行

There are cases when it doesn't run though


在已处理的异常中,可以保证关联的finally块可以运行
但是,如果未处理异常,则
finally块的执行取决于
触发异常展开操作的方式
。反过来,这取决于计算机的设置。

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up.

这里是重要部分


通常,当未处理的异常结束应用程序时,是否运行
而不是finally块并不重要。 但是,如果在即使在那种情况下都必须运行的finally块中有
语句,则
的一种解决方案是在try-finally语句中添加catch块。
或者,您可以捕获可能在调用堆栈上方的try-finally语句的
try块中引发的异常。

这篇关于为什么抛出异常后最终块可能无法执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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