CKTIS / PHP
LogicException
LogicException représente une utilisation erronée d’un objet ou la violation d’un invariant du programme.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
throw new LogicException("already sealed")LogicException représente une utilisation erronée d’un objet ou la violation d’un invariant du programme. Un catch de cette classe intercepte aussi DomainException et InvalidArgumentException. Le premier scellement est permis, mais le deuxième appel échoue sur la condition explicitement contrôlée.
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
Exemples
<?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); }
Résultat
already sealed DomainException