Baza znanjaIzpitno središče
SLSlovenščina
Izberi jezik13

CKTIS / PHP

DomainException

DomainException je LogicException za vrednost zunaj domene operacije.

Različica: PHP 8.4; repository runtime, production runtime not attested

Sintaksa

throw new DomainException("outside scale")

DomainException je LogicException za vrednost zunaj domene operacije. Funkcija sprejema cela števila od 0 do vključno 100 in izrecno zavrne 101. Niz prej pade na strogem preverjanju tipa s TypeError. Sam tip int ne določa intervala.

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

Primeri

<?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

Viri