set_exception_handler

本文演示了如何在PHP中使用静态类方法作为全局异常处理器。通过设置自定义的异常处理函数,可以实现更灵活的错误捕捉与处理机制。

https://www.php.net/manual/zh/function.set-exception-handler.php

 

class A
{
    public static function exception_handler($exception)
    {
        echo "静态类处理异常:",$exception->getMessage(), "\n";
    }
}
function exception_handler($exception) {
    echo "Uncaught exception: " , $exception->getMessage(), "\n";
}

set_exception_handler('A::exception_handler');

throw new Exception('Uncaught Exception2');
echo "Not Executed\n";
C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:18: error: variable or field 'custom_exception_handler' declared void 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:43: error: 'EXCEPTION_INFO' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:59: error: 'exc_info' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:17: error: variable or field 'custom_exception_handler' declared void 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:42: error: 'EXCEPTION_INFO' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:61:58: error: 'exc_info' was not declared in this scope 61 | extern "C" void custom_exception_handler(EXCEPTION_INFO* exc_info) { | ^~~~~~~~ C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino: In function 'void setup()': C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:143:27: error: 'custom_exception_handler' was not declared in this scope; did you mean '_xtos_set_exception_handler'? 143 | set_exception_handler(custom_exception_handler); | ^~~~~~~~~~~~~~~~~~~~~~~~ | _xtos_set_exception_handler C:\Users\Administrator\Documents\Arduino\sketch_jun18a_copy_20250620142542_copy_20250706090617\sketch_jun18a_copy_20250620142542_copy_20250706090617.ino:143:5: error: 'set_exception_handler' was not declared in this scope; did you mean '_xtos_set_exception_handler'? 143 | set_exception_handler(custom_exception_handler); | ^~~~~~~~~~~~~~~~~~~~~ | _xtos_set_exception_handler exit status 1 Compilation error: variable or field 'custom_exception_handler' declared void
最新发布
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值