也许下面的解释会更好地帮助你理解它是如何工作的:
try {
function1();//this might throw an exception
function2();//if we want function2 to be executed regardless
//if an exception was thrown from function1() - this
//is not a good place to call it!
} catch (Exception $e) {
echo $e->getMessage();
} finally {
function2();//then the right place to write it will be in a finally clause.
}
当从 - 抛出异常时function1()-function2()将不会执行 - 执行将“跳转”到 catch 部分。如果我们想要function2()执行而不管是否抛出错误,例如,如果function1()打开到 DB 的连接并运行一些选择并function2()关闭该连接,那么我们最好将调用放在后面function2()的finally块中catch