终点是什么意思? [英] What is the point of the finally block?

查看:364
本文介绍了终点是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了

try {
}
catch() {
}
finally {
    x = 3;
}

try {
}
catch() {
}

x = 3;

编辑:在.NET 2.0?

edit: in .NET 2.0?

所以

try {
    throw something maybe
    x = 3
}
catch (...) {
    x = 3
}

在行为上是等同的?

推荐答案

取决于语言,因为可能会有一些轻微的语义差异,但是这个想法是,它总是执行(几乎),即使try块中的代码抛出异常。

Depends on the language as there might be some slight semantic differences, but the idea is that it will execute (almost) always, even if the code in the try block threw an exception.

在第二个例子中,如果catch块返回或退出,x = 3将不被执行。在第一个它将。

In the second example, if the code in the catch block returns or quits, the x = 3 will not be executed. In the first it will.

在.NET平台中,在某些情况下,finally块的执行不会发生:
安全异常,线程挂起,电脑关机:)等。

In the .NET platform, in some cases the execution of the finally block won't occur: Security Exceptions, Thread suspensions, Computer shut down :), etc.

这篇关于终点是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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