CKTIS / PHP
LogicException
LogicException označuje nesprávné použití objektu nebo porušení invariantu programu.
Verze: PHP 8.4; repository runtime, production runtime not attested
Syntaxe
throw new LogicException("already sealed")LogicException označuje nesprávné použití objektu nebo porušení invariantu programu. Zachycení této třídy zahrnuje i potomky DomainException a InvalidArgumentException. První zapečetění je platné a druhé poruší podmínku výslovně ověřovanou kódem.
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
Pří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ýsledek
already sealed DomainException