set_exception_handler

今天在cl框架源码中看见关于错误类的设置,其中set_exception_handler方法未操作过,顺便学习一下。

set_exception_handler:该函数设置用户自定义的异常处理函数。

1
2
3
4
5
6
7
8
9
<?php
function showException($exception){
    echo "报错内容:".$exception->getMessage();
}
 
set_exception_handler('showException');//指定错误提示函数
 
 
throw new Exception('发生...程序错误');

打印内容:报错内容:发生...程序错误

在cl源码中,设置自定义报错提示,传递了数组的形式,看下示例。

set_exception_handler,如传递数组形式,第一个参数为class名,二为方法名。

注意:指定类中报错方法必须声明为静态方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
 
class App{
    static function customError($errno$errstr$errfile$errline) {
        echo "<b>自定义报错:</b> [$errno] $errstr<br />";
        echo "第几行 {$errline}在{$errfile}文件<br />";
        die();
    }
}
 
set_exception_handler(array("App","customError"));
 
$test=2;
 
if ($test > 1) {
    trigger_error("错误流程");
}

打印内容:

自定义报错: [1024] 错误流程

第几行 18在D:\phpstudy_pro\WWW\test\index.php文件

posted @   辉辉、  阅读(333)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示