美文网首页
PHP 抛出异常与报错的处理

PHP 抛出异常与报错的处理

作者: 云龙789 | 来源:发表于2019-02-18 19:27 被阅读2次

在使用 laravel 框架(或者其他框架)的时候,我们都知道在抛出异常报错的时候,其实这个异常并不像我们平时使用 throw new Exception('程序异常'); 抛出的页面那么难看,另外,在抛出异常后,框架会做异常日志记录。

<?php
// 注册全局异常处理程序
set_exception_handler(function (Exception $e) {
   // 处理并记录异常
});

// 注册其他的异常处理程序...

// 还原成之前的全局异常处理程序
restore_exception_handler();
demo-注意行号 打印结果
  • 注意
    所有的报错类都是集成 Exception 类的。所以,你如果需要打印错误信息,行号等信息,是可以直接调用 public 方法的。我在 demo 中是打印的报错对象
    在实际应用中,我们可以根据是否已经打开 debug 等配置,来决定怎么处理报错,并且可以在抛出报错之前可以将报错存入日志
class Exception implements Throwable {
    protected $message;
    protected $code;
    protected $file;
    protected $line;


    /**
     * Clone the exception
     * Tries to clone the Exception, which results in Fatal error.
     * @link https://php.net/manual/en/exception.clone.php
     * @return void
     * @since 5.1.0
     */
    final private function __clone() { }

    /**
     * Construct the exception. Note: The message is NOT binary safe.
     * @link https://php.net/manual/en/exception.construct.php
     * @param string $message [optional] The Exception message to throw.
     * @param int $code [optional] The Exception code.
     * @param Throwable $previous [optional] The previous throwable used for the exception chaining.
     * @since 5.1.0
     */
    public function __construct($message = "", $code = 0, Throwable $previous = null) { }

    /**
     * Gets the Exception message
     * @link https://php.net/manual/en/exception.getmessage.php
     * @return string the Exception message as a string.
     * @since 5.1.0
     */
    final public function getMessage() { }

    /**
     * Gets the Exception code
     * @link https://php.net/manual/en/exception.getcode.php
     * @return mixed|int the exception code as integer in
     * <b>Exception</b> but possibly as other type in
     * <b>Exception</b> descendants (for example as
     * string in <b>PDOException</b>).
     * @since 5.1.0
     */
    final public function getCode() { }

    /**
     * Gets the file in which the exception occurred
     * @link https://php.net/manual/en/exception.getfile.php
     * @return string the filename in which the exception was created.
     * @since 5.1.0
     */
    final public function getFile() { }

    /**
     * Gets the line in which the exception occurred
     * @link https://php.net/manual/en/exception.getline.php
     * @return int the line number where the exception was created.
     * @since 5.1.0
     */
    final public function getLine() { }

    /**
     * Gets the stack trace
     * @link https://php.net/manual/en/exception.gettrace.php
     * @return array the Exception stack trace as an array.
     * @since 5.1.0
     */
    final public function getTrace() { }

    /**
     * Returns previous Exception
     * @link https://php.net/manual/en/exception.getprevious.php
     * @return Exception the previous <b>Exception</b> if available
     * or null otherwise.
     * @since 5.3.0
     */
    final public function getPrevious() { }

    /**
     * Gets the stack trace as a string
     * @link https://php.net/manual/en/exception.gettraceasstring.php
     * @return string the Exception stack trace as a string.
     * @since 5.1.0
     */
    final public function getTraceAsString() { }

    /**
     * String representation of the exception
     * @link https://php.net/manual/en/exception.tostring.php
     * @return string the string representation of the exception.
     * @since 5.1.0
     */
    public function __toString() { }

    public function __wakeup() { }
}

报错处理

<?php

set_error_handler(//error handler function
    function ($errno, $errstr, $errfile, $errline) {
        echo "错误编号: $errno<br>";
        echo "错误信息: $errstr<br>";
        echo "出错文件: $errfile<br>";
        echo "出错行号: $errline<br>";
        die;
    });

echo $arr[1];
image.png image.png

相关文章

  • PHP 抛出异常与报错的处理

    laravel学院-最佳实践系列(十一):深入探讨 PHP 错误异常处理机制及 Laravel 框架底层的相应实现...

  • php 异常处理

    从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw抛出,异常抛出...

  • PHP学习笔记 - 进阶篇(10)

    异常处理 抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过...

  • PHP异常

    抛出一个异常 从PHP5开始,PHP支持异常处理,异常处理是面向对象一个重要特性,PHP代码中的异常通过throw...

  • 1.10 PHP异常处理

    1.抛出一个异常 从PHP5开始,PHP支持 异常处理 ,异常处理是面向对象的一个重要特性,PHP代码中的异常通过...

  • php源码-try、catch过程-原理

    在php中我们通过try catch 捕获异常, 通过 throw来抛出异常, 优秀文章推荐:异常处理 先来说说...

  • (五)C++中的异常处理与模板类

    C++中的异常处理与模板类 一、C++ 中的异常处理 1、异常处理 在C++ 中可以抛出任何类型的异常,根据抛出的...

  • thinkphp5.0 助手函数大全

    load_trait:快速导入Traits,PHP5.5以上无需调用 exception:抛出异常处理 debug...

  • php错误与异常处理方法

    php与其他计算机编程语言在遇到错误就抛出异常不太一样,PHP在处理对象时它也有异常机制,但是PHP会尽可能的愉快...

  • Java 常用工具类--Java异常

    处理程序运行阶段的错误,不是编译阶段。 异常处理分类 抛出异常 捕获异常image.png throw与throw...

网友评论

      本文标题:PHP 抛出异常与报错的处理

      本文链接:https://www.haomeiwen.com/subject/pkezeqtx.html