PHP8.3 Error和Exception异常树列表

PHP技术
173
0
0
2024-01-16
标签   PHP8

简介

Throwable 是能被 throw 语句抛出的最基本的接口(interface),包含了 Error 和 Exception 。

注意:PHP 类无法直接实现 (implement) Throwable 接口,而应当去继承 Exception。

语法

Error

简介

Error 是所有 PHP 内部错误类的基类。

类摘要

<?php

class Error implements Throwable {

    /* 属性 */
    protected string $message = "";
    private string $string = "";
    protected int $code;
    protected string $file = "";
    protected int $line;
    private array $trace = [];
    private ?Throwable $previous = null;

    /* 方法 */
    public __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
    final public getMessage(): string
    final public getPrevious(): ?Throwable
    final public getCode(): int
    final public getFile(): string
    final public getLine(): int
    final public getTrace(): array
    final public getTraceAsString(): string
    public __toString(): string
    private __clone(): void
}

方法目录

  • Error::__construct — 初始化 error 对象
  • Error::getMessage — 获取错误信息
  • Error::getPrevious — 返回先前的 Throwable
  • Error::getCode — 获取错误代码
  • Error::getFile — 获取错误发生时的文件
  • Error::getLine — 获取错误发生时的行号
  • Error::getTrace — 获取调用栈(stack trace)
  • Error::getTraceAsString — 获取字符串形式的调用栈(stack trace)
  • Error::__toString — error 的字符串表达
  • Error::__clone — 克隆 error

Exception

简介

Exception 是所有用户级异常的基类。

类摘要

<?php

class Exception implements Throwable {

    /* 属性 */
    protected string $message = "";
    private string $string = "";
    protected int $code;
    protected string $file = "";
    protected int $line;
    private array $trace = [];
    private ?Throwable $previous = null;

    /* 方法 */
    public __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
    final public getMessage(): string
    final public getPrevious(): ?Throwable
    final public getCode(): int
    final public getFile(): string
    final public getLine(): int
    final public getTrace(): array
    final public getTraceAsString(): string
    public __toString(): string
    private __clone(): void
}

方法目录

  • Exception::__construct — 异常构造函数
  • Exception::getMessage — 获取异常消息内容
  • Exception::getPrevious — 返回前一个 Throwable
  • Exception::getCode — 获取异常代码
  • Exception::getFile — 创建异常时的程序文件名称
  • Exception::getLine — 获取创建的异常所在文件中的行号
  • Exception::getTrace — 获取异常追踪信息
  • Exception::getTraceAsString — 获取字符串类型的异常追踪信息
  • Exception::__toString — 将异常对象转换为字符串
  • Exception::__clone — 异常克隆

树列表

下面是一个快速图表,可帮助您概览 PHP 版本中使用的异常

Throwable
  ├── Error
  │     ├── ArithmeticError
  │     │       └── DivisionByZeroError
  │     ├── AssertionError
  │     ├── CompileError
  │     │       └── ParseError
  │     ├── TypeError
  │     │       └── ArgumentCountError
  │     ├── ValueError
  │     ├── UnhandledMatchError
  |     ├── FiberError
  |     └── Random\RandomError
  │     │       └── BrokenRandomEngineError
  |     └── DateError
  |             ├── DateObjectError
  │             └── DateRangeError
  └── Exception
        ├── ClosedGeneratorException
        ├── DOMException
        ├── ErrorException
        ├── IntlException
        ├── JsonException
        ├── LogicException
        │       ├── BadFunctionCallException
        │       │        └── BadMethodCallException
        │       ├── DomainException
        │       ├── InvalidArgumentException
        │       ├── LengthException
        │       └── OutOfRangeException
        ├── PharException
        ├── ReflectionException
        ├── RuntimeException
        │       ├── OutOfBoundsException
        │       ├── OverflowException
        │       ├── PDOException
        │       ├── RangeException
        │       ├── UnderflowException
        │       ├── UnexpectedValueException
        │       └── PDOException
        ├── SodiumException
        ├── FiberExit
        ├── Random\RandomException
        └── DateException
                ├── DateInvalidTimeZoneException
                ├── DateInvalidOperationException
                ├── DateMalformedStringException
                ├── DateMalformedIntervalStringException
                └── DateMalformedPeriodStringException

PHP 8.3

PHP 8.3 是即将发布的 PHP 版本的第一个候选版本,现在可以进行测试。

第一个普遍可用的 PHP 版本定于今年 11 月 23 日发布,PHP 8.3 的第一个候选版本是发布前的一个重要里程碑,因为它表明 PHP 8.3 的所有更改现在都已实现,只剩下抛光和错误修复。

在 11 月 23 日 PHP 8.3.0 正式发布之前,还会有五个候选版本,每个版本在两周后发布。

PHP 8.3.0RC1 在 PHP-src 存储库中被标记,现在有一个 PHP-8.3 分支将用于剪切更多的 PHP 8.3 版本。该存储库的主分支现在正在为 2024 年发布的 PHP 8.4 版本进行准备和开发。