set_exception_handler 自定义异常处理

本文介绍了PHP中set_exception_handler函数的使用方法,通过示例展示了如何设置自定义异常处理函数,以便在程序出现未捕获异常时进行特定处理。

刚才已经说过了set_error_handler这个函数,作用就是自定义错误处理,

那么现在就来简单的说一下set_exception_handler,看名字我们就能发现,这说的是自定义异常处理。

呵呵,我聪明吧?来,先看一下调用方法:

string set_exception_handler ( callback $exception_handler )

同样是在看TP代码的时候发现的这个函数,就想不明白了,自己以前咋就没关心过这些东西呢?(捶胸顿足ing。。。) 继续来看一下TP是怎么实现的,呃,为啥一定要用TP呢,嗯。那好吧,一会儿我把手册的例子也搬过来。 public function appException($e) { halt($e->__toString()); } set_exception_handler(array(&$this,"appException")); 呵呵,这个简单吧?因为我没有给出halt这个方法的实现,这样就足够了。 再看手册中的例子, function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "/n"; } set_exception_handler('exception_handler'); throw new Exception('Uncaught Exception'); echo "Not Executed/n"; 其实呢,set_exception_handler就是设置了一下,当你的程序需要抛出一个异常的时候调用哪个自定义的函数。 就这么简单

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、付费专栏及课程。

余额充值