我在PHP中捕获异常时遇到问题如何从另一个类方法中捕获异常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块的函数内并调用该函数,我能够赶上例外没有问题。
我在做什么错?
把你的try/catch块中调用代码 –
http://stackoverflow.com/questions/5369488/try-catch-cannot -with-require-once-in-php –
[Can not Reproduce](http://3v4l.org/afJjt)你确定提供的代码示例是被调用的吗? *(可能是子类)*你得到的异常消息似乎与应该抛出的不匹配。 *(未评估的$行动)* – Touki