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;
}