CKTIS / PHP
LogicException
LogicException bezeichnet einen Fehler bei der Objektverwendung oder eine verletzte Programminvariante.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
throw new LogicException("already sealed")LogicException bezeichnet einen Fehler bei der Objektverwendung oder eine verletzte Programminvariante. Ein catch dieser Klasse erfasst auch Unterklassen wie DomainException und InvalidArgumentException. Das erste Versiegeln ist erlaubt, beim zweiten Aufruf erkennt der ausdrücklich geprüfte Zustand den Fehler.
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
Beispiele
<?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); }
Ergebnis
already sealed DomainException