CKTIS / PHP
DomainException
DomainException je LogicException za vrijednost izvan domene operacije.
Verzija: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
throw new DomainException("outside scale")DomainException je LogicException za vrijednost izvan domene operacije. Funkcija prihvaća cijele brojeve od 0 do uključivo 100 i izričito odbija 101. Niz znakova ne prolazi raniju strogu provjeru tipa i daje TypeError. Sam tip int ne određuje 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
Primjeri
<?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;
}
Rezultat
0 100 DomainException TypeError