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

2017-09-15  本文已影响4人  子木同
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
上一篇 下一篇

猜你喜欢

热点阅读