【发布时间】:2013-06-25 08:50:20
【问题描述】:
考虑这两个例子
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
}
some_code();
// More arbitrary code
?>
和
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
} finally {
some_code();
}
// More arbitrary code
?>
有什么区别?是否存在第一个示例不会执行some_code() 而第二个示例执行的情况?我完全没有抓住重点吗?
【问题讨论】:
-
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. -
我认为这与此线程有关:stackoverflow.com/questions/15031515/…
-
只是为了保存其他人从 PHP 手册中复制粘贴,我已经阅读了但不明白这两个示例之间的区别,否则我不会问这个问题。
-
查看 RFC 文档:wiki.php.net/rfc/finally
-
旁注: finally 块可用于 PHP5.5+
标签: php try-catch-finally finally