PHP 5.5,最后尝试... [英] PHP 5.5 and try ... finally

查看:81
本文介绍了PHP 5.5,最后尝试...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP 5.5在try / catch块中添加了 finally 的支持。

PHP 5.5 is adding support for finally in try/catch blocks.

Java允许您创建一个没有catch块的try / catch / finally块,所以当异常发生时,您可以在本地进行清理,但是让异常本身传播调用堆栈,因此可以单独处理。

Java allows you to create a try/catch/finally block with no catch block, so you can cleanup locally when an exception happens, but let the exception itself propagate up the call stack so it can be dealt with separately.

try {
    // Do something that might throw an exception here
} finally {
    // Do cleanup and let the exception propagate
}

在当前版本的PHP中可以实现一些可以对异常进行清理并让它传播的东西,但是如果没有抛出异常,那么清理代码就不会被调用。

In current versions of PHP you can achieve something that can do cleanup on an exception and let it propagate, but if no exception is thrown then the cleanup code is never called.

try {
    // Do something that might throw an exception here
} catch (Exception $e) {
    // Do cleanup and rethrow
    throw $e;
}

PHP 5.5是否支持try / finally样式?我已经找到了关于这个的信息,但是最接近我从PHP.net找到一个答案,只是暗示没有。

Will PHP 5.5 support the try/finally style? I have looked for information on this, but the closest I could find to an answer, from PHP.net, only implies that it doesn't.


在PHP 5.5及更高版本中,还可以在
catch块之后指定finally块。 finally块中的代码总是在try和catch块之后执行
,无论异常是否抛出
,并且在正常执行恢复之前。

In PHP 5.5 and later, a finally block may also be specified after the catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

该措辞表明,您总是希望有一个catch块,但它并没有直截了当,我可以看到。

The wording suggests that you're always expected to have a catch block, but it doesn't state it outright as far as I can see.

推荐答案

是, try / finally 被支持( RFC 实时代码)。文件确实不是很清楚,应该修改。

Yes, try/finally is supported (RFC, live code). The documentation is indeed not very clear and should be amended.

这篇关于PHP 5.5,最后尝试...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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