[PHP错误异常]①⑦--像处理异常一样处理PHP错误

Paste_Image.png

ErrorException.php

<?php
function exception_error_handler($errno, $errstr, $errfile, $errline)
{
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

set_error_handler('exception_error_handler');
try {
    echo gettype();
} catch (Exception $e) {
    $e->getMessage();
}

ErrorToException.php

<?php

class ErrorToException extends Exception
{
    public static function handle($errno, $errstr)
    {
        throw new self($errstr, $errno);
    }
}

//set_error_handler(array('ErrorToException', 'handle'));
set_error_handler(array('ErrorToException', 'handle'), E_USER_WARNING | E_WARNING);

try {
    echo $test;
    echo "<hr/>";
    gettype();
    echo "<hr/>";
    //trigger_error('test', E_USER_WARNING);
} catch (Exception $e) {
    echo '异常<br/>';
    echo $e->getMessage();
}
?>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • PHP 学习目录 ├─PHP视频教程 1 LAMP网站构建 │ ├─PHP教程 1.1.1 新版视频形式介绍│ ...
    曹渊说创业阅读 16,175评论 29 417
  • Exception_Observer.php Logging_Exception_Observer.php Ema...
    子木同阅读 244评论 1 0
  • 本文介绍PHP的异常,错误以及如何屏蔽错误。参考:php异常、错误处理机制、PHP错误提示的关闭方法详解 首先要明...
    刘振阅读 1,297评论 0 8
  • 他带着耳机,哼着歌;她抬头一看,微笑的脸,相视而笑。他坐下来把另一支耳机给她,一起听歌,一起聊歌,一起学习,毅的声...
    共享满天繁星阅读 270评论 0 0
  • 小时候,非常盼望过年,不仅有新衣服,且可向父母要求到集市上买个心仪的玩具,最让我开心的,还是有很多东西可以吃,特别...
    张春华阅读 360评论 0 0