我无法在 PHP 中捕获异常
这是我的代码。
try {
require $this->get_file_name($action);
}
catch (Exception $e) {
//do something//
}
和被调用的方法
private function get_file_name($action) {
$file = '../private/actions/actions_'.$this->group.'.php';
if (file_exists($file) === false) {
throw new Exception('The file for this '.$action.' was not found.');
}
else {
return $file;
}
}
导致:
Fatal error: Uncaught exception 'Exception' with message $action was not found.'
Exception: The file for this $action was not found.
但是,如果我在函数内部放置一个 try-catch 块并调用该函数,我能够捕获异常没有问题。
我究竟做错了什么?