gpt4 book ai didi

PHP 5.5 并尝试......终于

转载 作者:可可西里 更新时间:2023-10-31 23:07:27 26 4
gpt4 key购买 nike

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

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

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

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

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 找到最接近答案的信息,只是暗示它没有。

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 block ,但据我所知,它并没有直接说明。

最佳答案

是的,支持 try/finally(RFClive code)。文档确实不是很清楚,应该修改。

关于PHP 5.5 并尝试......终于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17208838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com