CKTIS / PHP
LogicException
LogicException assinala uso incorreto de um objeto ou violação de uma invariante do programa.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
throw new LogicException("already sealed")LogicException assinala uso incorreto de um objeto ou violação de uma invariante do programa. Um catch desta classe também captura DomainException e InvalidArgumentException. A primeira selagem é válida e a segunda viola a condição que o programa verifica explicitamente.
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
Exemplos
<?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); }
Resultado
already sealed DomainException