2024 PHP Conference Japan

例外 (Exception)

(PHP 5, PHP 7, PHP 8)

簡介

Exception 是所有使用者例外 (Exception) 的基底類別。

類別概要

class Exception implements Throwable {
/* 屬性 */
protected 字串 $message = "";
私有 (private) 字串 (string) $string = "";
保護 (protected) 整數 (int) $code;
保護 (protected) 字串 (string) $file = "";
保護 (protected) 整數 (int) $line;
私有 (private) 陣列 (array) $trace = [];
私有 (private) 可為空 (nullable) Throwable $previous = null;
/* 方法 (Methods) */
公開 (public) __construct(字串 (string) $message = "", 整數 (int) $code = 0, 可為空 (nullable) Throwable $previous = null)
最終 (final) 公開 (public) getMessage(): 字串 (string)
最終 (final) 公開 (public) getPrevious(): 可為空 (nullable) 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 (無返回值)
}

屬性 (Properties)

message

例外訊息

code

例外代碼

file

產生例外的檔案名稱

產生例外錯誤的行數

前一個

先前拋出的例外錯誤

字串

堆疊追蹤的字串表示

追蹤

堆疊追蹤,以陣列形式呈現

目錄

新增註解

使用者貢獻的註解 3 則註解

whysteepy at gmail dot com
6 年前
7.2.0 版本的 Throwable 和 Exception 樹狀結構列表

Error
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
ArgumentCountError
例外 (Exception)
ClosedGeneratorException
DOMException
ErrorException
IntlException
LogicException
BadFunctionCallException
BadMethodCallException
DomainException
InvalidArgumentException
LengthException
OutOfRangeException
PharException
ReflectionException
RuntimeException
OutOfBoundsException
OverflowException
PDOException
RangeException
UnderflowException
UnexpectedValueException
SodiumException

在以下連結中找到腳本和輸出
https://gist.github.com/mlocati/249f07b074a0de339d4d1ca980848e6a
https://3v4l.org/sDMsv

由這裡的某人發佈 https://php.dev.org.tw/manual/en/class.throwable.php
cHao
10 年前
請注意,例外錯誤的屬性是在例外錯誤*建立*時填入的,而不是在拋出時。拋出例外錯誤似乎不會修改它們。

除此之外,這意味著

* 例外錯誤會指向建立它的行,而不是拋出它的行。

* 與某些其他語言不同,重新拋出例外錯誤不會弄亂追蹤資訊。

* 拋出的例外錯誤和未拋出的例外錯誤看起來基本相同。在我的機器上,唯一可見的區別是拋出的例外錯誤具有 `xdebug_message` 屬性,而未拋出的例外錯誤則沒有。當然,如果您沒有安裝 xdebug,您甚至不會看到這個屬性。
shaman_master at list dot ru
4 年前
注意:此文件不完整,ReflectionObject::export($exception)
<?php
類別
class [ class Exception implements Throwable ] 的物件 {
- 屬性
[7] {
屬性 [ protected $message ]
屬性 [ private $string ]
屬性 [ protected $code ]
屬性 [ protected $file ]
屬性 [ protected $line ]
屬性 [ private $trace ]
屬性 [ private $previous ]
}
- 方法
[11] {
方法 [ final private method __clone ] {
}

方法 [ public method __construct ] {

- 參數
[3] {
參數 #0 [ $message ]
參數 #1 [ $code ]
參數 #2 [ $previous ]
}
}

方法 [ public method __wakeup ] {
}

方法 [ final public method getMessage ] {
}

方法 [ final public method getCode ] {
}

方法 [ final public method getFile ] {
}

方法 [ final public method getLine ] {
}

方法 [ final public method getTrace ] {
}

方法 [ final public method getPrevious ] {
}

方法 [ final public method getTraceAsString ] {
}

方法 [ public method __toString ] {
}
}
}
?>

遺漏

屬性 [ private $string ]
屬性 [ private $trace ]
屬性 [ private $previous ]

方法 [ public method __wakeup ] {
}
To Top