php - 未捕获的 PHP 异常
问题描述
我有以下代码:
set_error_handler(
function ($errno, $errstr, $errfile, $errline, array $errcontex) {
throw new Exception($errstr, 0);
},
E_ALL
);
class Foo
{
public function asd(): int
{
return 0;
}
}
class Bar
{
public function asd(): int
{
return 1;
}
}
$a = [
1 => new Foo(),
2 => new Bar(),
];
try {
array_diff([], $a);
} catch (Throwable $exception)
{
var_dump("11");
}
当我运行它时,它会导致以下错误:PHP Warning: Uncaught Exception: Object of class Foo could not be converted to string in /oms/bebe.php:5 Stack trace: ...
. 也就是说,尽管存在异常,但似乎没有被捕获set_error_handler
。更奇怪的是,当我声明一个$a
包含单个元素的数组时,
$a = [
2 => new Bar(),
];
一切正常,即捕获异常并11
输出。
怎么来的?以及如何用任意数组元素数量捕获该异常?
解决方案
推荐阅读
- amazon-web-services - 如何为不同 AWS 账户中 3 个不同区域的 3 个不同 VPC 设置 VPN?
- c# - 如何使用后面的“c#”代码调用网页网址
- c# - 用于比较数据库模式和数据的应用程序
- bash - 如何区分shell脚本中的两个文件
- swift - Swift - 为什么即使 where 子句专门化了关联类型,协议仍被视为泛型?
- javascript - 错误:类型时间戳的输入语法无效
- visual-studio-2017 - 安装 Microsoft SQL Server Management Studio 2017 并构建项目后,项目无法正常工作
- paypal - 添加折扣产品时的 PayPal 问题
- c# - Selenium 驱动程序在保存文件时出现问题
- debugging - Kafka Connect 分离日志记录