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

标签 php exception exception-handling

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/

相关文章:

java - ExpectedException 的原因是什么?

oracle - 在异常处理程序中查找作业编号

php - 使用 Guzzle 检查远程文件是否存在的最佳方法是什么?

php artisan 迁移错误 PDOEXception 访问被拒绝 laravel 5

PHPExcel 输出不可读并且没有抛出错误

java - 未处理的异常的输出与程序的输出混淆

c# - AppDomain 未处理的异常

php - 从 FPDF 中的 HTML 字符生成符号

Java并发实践: BoundedExecutor implementation

java - Android 中无法使用 JAVA 解决异常处理程序?