
Throwable 是能被 throw 语句抛出的最基本的接口(interface),包含了 Error 和 Exception 。
注意:PHP 类无法直接实现 (implement) Throwable 接口,而应当去继承 Exception。
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
}
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
}
下面是一个快速图表,可帮助您概览 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 版本的第一个候选版本,现在可以进行测试。
第一个普遍可用的 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 版本进行准备和开发。