CKTIS / PHP
LogicException
LogicException segnala uso scorretto di un oggetto o violazione di un invariante del programma.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
throw new LogicException("already sealed")LogicException segnala uso scorretto di un oggetto o violazione di un invariante del programma. Un catch della classe intercetta anche DomainException e InvalidArgumentException. La prima sigillatura è permessa, la seconda viola la precondizione verificata esplicitamente.
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
Esempi
<?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); }
Risultato
already sealed DomainException