CKTIS / PHP
DomainException
DomainException je LogicException pre hodnotu mimo definičného oboru operácie.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
throw new DomainException("outside scale")DomainException je LogicException pre hodnotu mimo definičného oboru operácie. Funkcia prijíma celé čísla od 0 do 100 vrátane a výslovne odmietne 101. Reťazec zlyhá skôr pri prísnej 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
Prí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ýsledok
0 100 DomainException TypeError