这个问题是关于仅在没有抛出异常的情况下在 try 块之外执行代码的最佳方法。
try {
//experiment
//can't put code after experiment because I don't want a possible exception from this code to be caught by the following catch. It needs to bubble.
} catch(Exception $explosion) {
//contain the blast
} finally {
//cleanup
//this is not the answer since it executes even if an exception occured
//finally will be available in php 5.5
} else {
//code to be executed only if no exception was thrown
//but no try ... else block exists in php
}
这是@webbiedave针对问题php try .. else建议的方法。由于使用了额外的$caught
变量,我觉得它不令人满意。
$caught = false;
try {
// something
} catch (Exception $e) {
$caught = true;
}
if (!$caught) {
}
那么,在不需要额外变量的情况下,有什么更好(或最好)的方法来实现这一点?