Znalostná bázaCentrum skúšok
SKSlovenčina
Vybrať jazyk13

CKTIS / PHP

LogicException

LogicException označuje nesprávne použitie objektu alebo porušenie invariantu programu.

Verzia: PHP 8.4; repository runtime, production runtime not attested

Syntax

throw new LogicException("already sealed")

LogicException označuje nesprávne použitie objektu alebo porušenie invariantu programu. Zachytenie tejto triedy zahŕňa aj potomkov DomainException a InvalidArgumentException. Prvé zapečatenie je platné a druhé poruší podmienku výslovne overovanú kódom.

class Envelope {
    private bool $sealed = false;
    public function seal(): void {
        if ($this->sealed) { throw new LogicException("already sealed"); }
        $this->sealed = true;
    }
}
$e = new Envelope(); $e->seal();
try { $e->seal(); } catch (LogicException $x) { echo $x->getMessage(), PHP_EOL; }
try { throw new DomainException("range"); } catch (LogicException $x) { echo get_class($x); }
already sealed
DomainException

Príklady

<?php
class Envelope {
    private bool $sealed = false;
    public function seal(): void {
        if ($this->sealed) { throw new LogicException("already sealed"); }
        $this->sealed = true;
    }
}
$e = new Envelope(); $e->seal();
try { $e->seal(); } catch (LogicException $x) { echo $x->getMessage(), PHP_EOL; }
try { throw new DomainException("range"); } catch (LogicException $x) { echo get_class($x); }

Výsledok

already sealed
DomainException

Zdroje