php - 如何从调用的函数中捕获错误?
问题描述
好的,我有问题,catch
或者error
当exception
我打电话给undefined
function
我有一个调用函数的方法,有两个参数(bool, function)
private function invoking($hdr = true, $fnc) {
if(is_callable($fnc)) {
if($hdr) {
$this->load_header();
}
try {
$fnc->__invoke();
} catch(Exception $er) {
echo "Something went wrong. ".$er;
}
} else {
echo "function not callable";
}
}
但是,我有一个问题来捕捉里面的错误$fnc
。
$this->invoking(true, function() {
undefinedfunction();
// for example i called this, which the function doesnt exist
});
但是似乎 catch 对 i 内部的内容不起作用__invoke()
,我应该怎么做才能捕获invoked
函数内部的错误?
感谢您的任何建议
解决方案
但似乎捕获不适用于 i __invoke() 内部的内容
它不起作用,因为它抛出了一个Fatal error
无法使用Exception
类处理的问题。在 PHP 7 之前,几乎不可能捕获这些错误。
在 PHP 7 中:
现在大多数错误都是通过抛出错误异常来报告的
阅读更多关于PHP 7 中的错误
因此,如果您的 php 版本 >= PHP 7,您可以这样做
try {
$fnc->__invoke();
} catch(Error $er) { // Error is the base class for all internal PHP errors
echo $er->getMessage();
}
推荐阅读
- c# - System.ArgumentException:“encryptedTicket”参数的无效值发生在第一次成功登录后的任何登录上
- .net - Web 应用程序转换后将构建操作更改为“页面”而不是“内容”
- variables - WinBUGS 中未初始化的变量和#Bugs:dic.set 无法执行(灰显)问题
- php - Symfony 控制器功能测试中的 $this->getUser() 返回 null
- python - 检查元组列表的顺序
- amazon-web-services - 如何像 SQS 一样使用 Kinesis?
- javascript - Window.whatever 未定义
- c# - 如何格式化 Dynamics 365 c# 插件的 LINQ 查询
- typescript - `keyof T`和`Extract有什么区别
` 使用扩展泛型时? - python - 如何在没有循环和 if-else 的情况下打印整个圣诞节十二天的歌词