2024 年日本 PHP 研討會

RangeException 類別

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

簡介

程式執行期間,用於指出範圍錯誤的例外。通常這表示發生了除上溢位/下溢位之外的算術錯誤。這是 DomainException 的執行階段版本。

類別概要

class RangeException extends RuntimeException {
/* 繼承的屬性 */
protected string $message = "";
private string $string = "";
protected int $code;
保護的 字串 $file = "";
保護的 整數 $line;
私有的 陣列 $trace = [];
私有的 ?Throwable $previous = null;
/* 繼承的方法 */
公開的 Exception::__construct(字串 $message = "", 整數 $code = 0, ?Throwable $previous = null)
最終 公開的 Exception::getMessage(): 字串
最終 公開的 Exception::getCode(): 整數
最終 公開的 Exception::getFile(): 字串
最終 公開的 Exception::getLine(): 整數
最終 公開的 Exception::getTrace(): 陣列
}
新增註釋

使用者貢獻的註釋 1 則註釋

2
evguenia dot chagnon at gmail dot com
7 年前
CF. 網域例外 (DomainException):DomainException 與 RangeException 相似,我們應在類似的情況下使用它們。但前者設計用於確定問題出在我們的專案、第三方元件等時(簡言之:邏輯錯誤),後者設計用於確定問題出在輸入資料或環境時(簡言之:執行時期錯誤)。

function divide($divident, $input) {
if(!is_numeric($divident) || !is_numeric($input)) {
throw new InvalidArgumentException("函式僅接受數值");
}
if($input == 0) {
throw new RangeException("除數不能為零");
}
return $divident / $input;
}
To Top