无法在 catch 块中捕获异常 - Yii2
Exception not able to get caught in catch block - Yii2
异常
Exception 'Error' with message 'Class 'app\commands\CallLogs' not
found'
无法被 catch 块捕获。
代码:
我尝试调用 undefined class 只是为了看看异常 catch 块是如何捕获的以及捕获了什么。
public function actionTest(){
try {
$logs = new CallLogs();
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
但是,当我故意抛出任何异常时,它会起作用。
public function actionTest(){
try {
throw new \yii\base\Exception('hello');
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
我试过 base\Exception class 和 \ErrorException class。但是,没有帮助。
任何 help/hint 都是可观的
catch (\Throwable $e)
会完成工作
\Throwable
是在 PHP 7.0 中引入的,并且(从文档中引用)用于
[...] any object that can be thrown via a throw statement, including
Error and Exception.
异常
Exception 'Error' with message 'Class 'app\commands\CallLogs' not found'
无法被 catch 块捕获。
代码:
我尝试调用 undefined class 只是为了看看异常 catch 块是如何捕获的以及捕获了什么。
public function actionTest(){
try {
$logs = new CallLogs();
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
但是,当我故意抛出任何异常时,它会起作用。
public function actionTest(){
try {
throw new \yii\base\Exception('hello');
} catch (\yii\base\Exception $ex) {
print $ex->getMessage();
} catch(\ErrorException $ex){
print $ex->getMessage();
}
}
我试过 base\Exception class 和 \ErrorException class。但是,没有帮助。
任何 help/hint 都是可观的
catch (\Throwable $e)
会完成工作
\Throwable
是在 PHP 7.0 中引入的,并且(从文档中引用)用于
[...] any object that can be thrown via a throw statement, including Error and Exception.