php错误和异常

180it 2019-09-23 AM 426℃ 0条

php错误和异常
在编码的时候,我们无时无刻会遇到错误和异常,所以我们需要处理这些错误。

php的错误类型有

E_ERROR 致命的错误。会中断程序的执行
E_WARNING 警告。不会中断程序
E_NOTICE 通知,运行时通知。表示脚本遇到可能会表现为错误的情况
E_PARSE 解析错误,一般是语法错误。
E_STRICT PHP 对代码的修改建议
E_DEPRECATED 将会对在未来版本中可能无法正常工作的代码给出警告
错误
在开发模式中,我们一般需要打开error_reporting 设置为E_ALL。然后把display_errors 设置为on

如果需要记录错误日志,则需要配置log_errors.

开发者可以 通过set_error_handle()自己接管错误。

set_error_handle(function($errno,$errstr,$errfile,$errline){

})

/**

  • throw exceptions based on E_* error types
    */

set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{

// error was suppressed with the @-operator
if (0 === error_reporting()) { return false;}
switch($err_severity)
{
    case E_ERROR:               throw new ErrorException            ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_WARNING:             throw new WarningException          ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_PARSE:               throw new ParseException            ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_NOTICE:              throw new NoticeException           ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_CORE_ERROR:          throw new CoreErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_CORE_WARNING:        throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_COMPILE_ERROR:       throw new CompileErrorException     ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_COMPILE_WARNING:     throw new CoreWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_USER_ERROR:          throw new UserErrorException        ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_USER_WARNING:        throw new UserWarningException      ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_USER_NOTICE:         throw new UserNoticeException       ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_STRICT:              throw new StrictException           ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_RECOVERABLE_ERROR:   throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_DEPRECATED:          throw new DeprecatedException       ($err_msg, 0, $err_severity, $err_file, $err_line);
    case E_USER_DEPRECATED:     throw new UserDeprecatedException   ($err_msg, 0, $err_severity, $err_file, $err_line);
}

});

class WarningException extends ErrorException {}
class ParseException extends ErrorException {}
class NoticeException extends ErrorException {}
class CoreErrorException extends ErrorException {}
class CoreWarningException extends ErrorException {}
class CompileErrorException extends ErrorException {}
class CompileWarningException extends ErrorException {}
class UserErrorException extends ErrorException {}
class UserWarningException extends ErrorException {}
class UserNoticeException extends ErrorException {}
class StrictException extends ErrorException {}
class RecoverableErrorException extends ErrorException {}
class DeprecatedException extends ErrorException {}
class UserDeprecatedException extends ErrorException {}
PHP7的错误处理
PHP 7 改变了大多数错误的报告方式。error可以通过exception异常进行捕获到.不能通过try catch捕获。但是可以通过注册到set_exception_handle捕获。

Throwable
Error
Exception
try
{
// Code that may throw an Exception or Error.
}
catch (Throwable $t)
{
// Executed only in PHP 7, will not match in PHP 5
}
catch (Exception $e)
{
// Executed only in PHP 5, will not be reached in PHP 7
}
异常
捕获异常可以通过try catch 语句

try{
//异常的代码
}catch (Exception $e){
//处理异常
}finally{
//最后执行的
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php错误和异常

上一篇 php变量和常量
下一篇 php运算符