Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

CKTIS / PHP

LogicException

LogicException indica uso incorrecto de un objeto o incumplimiento de un invariante del programa.

Versión: PHP 8.4; repository runtime, production runtime not attested

Sintaxis

throw new LogicException("already sealed")

LogicException indica uso incorrecto de un objeto o incumplimiento de un invariante del programa. Un catch de esta clase también captura DomainException e InvalidArgumentException. El primer sellado es válido y el segundo incumple la condición que el programa comprueba explícitamente.

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

Ejemplos

<?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

Fuentes