KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

LogicException

LogicException betekent onjuist gebruik van een object of schending van een programma-invariant.

Versie: PHP 8.4; repository runtime, production runtime not attested

Syntax

throw new LogicException("already sealed")

LogicException betekent onjuist gebruik van een object of schending van een programma-invariant. Een catch voor deze klasse vangt ook DomainException en InvalidArgumentException. De eerste verzegeling is geldig en de tweede schendt de expliciet getoetste voorwaarde.

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

Voorbeelden

<?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); }

Resultaat

already sealed
DomainException

Bronnen