CKTIS / PHP
DomainException
DomainException is a LogicException for a value outside an operation’s defined domain.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
throw new DomainException("outside scale")DomainException is a LogicException for a value outside an operation’s defined domain. The example accepts integers only in the closed interval from 0 to 100 and raises it explicitly for 101. A strict parameter type rejects a string earlier with TypeError. Choosing a PHP type alone does not encode the permitted interval.
declare(strict_types=1);
function percent(int $n): int {
if ($n < 0 || $n > 100) { throw new DomainException("outside scale"); }
return $n;
}
foreach ([0, 100, 101, "5"] as $n) {
try { echo percent($n); } catch (DomainException|TypeError $e) { echo get_class($e); }
echo PHP_EOL;
}
0
100
DomainException
TypeError
Examples
<?php
declare(strict_types=1);
function percent(int $n): int {
if ($n < 0 || $n > 100) { throw new DomainException("outside scale"); }
return $n;
}
foreach ([0, 100, 101, "5"] as $n) {
try { echo percent($n); } catch (DomainException|TypeError $e) { echo get_class($e); }
echo PHP_EOL;
}
Result
0 100 DomainException TypeError