CKTIS / PHP
DomainException
DomainException je LogicException pro hodnotu mimo definiční obor operace.
Verze: PHP 8.4; repository runtime, production runtime not attested
Syntaxe
throw new DomainException("outside scale")DomainException je LogicException pro hodnotu mimo definiční obor operace. Funkce přijímá celá čísla od 0 do 100 včetně a výslovně odmítá 101. Řetězec selže už při přísné kontrole typu s TypeError. Samotný typ int interval neurčuje.
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
Příklady
<?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;
}
Výsledek
0 100 DomainException TypeError