LogicException
PHPERRORCommonLogic
Logic error — represents a programming bug
Quick Answer
Throw subclasses of LogicException (InvalidArgumentException, BadMethodCallException, etc.) to signal programming mistakes.
What this means
Base SPL exception for errors that represent bugs in the program logic — conditions that should have been caught at development time. Use for precondition violations.
Why it happens
- 1Method called before required initialization
- 2Abstract method called incorrectly
Fix
Use specific LogicException subclass
Use specific LogicException subclass
class OrderProcessor {
private bool $initialized = false;
public function init(): void {
$this->initialized = true;
}
public function process(Order $order): void {
if (!$this->initialized) {
throw new \LogicException("Call init() before process()");
}
}
}Why this works
LogicExceptions signal bugs — do not catch and swallow them; let them surface to developers.
Code examples
Hierarchyphp
// LogicException hierarchy: // LogicException // BadFunctionCallException // BadMethodCallException // DomainException // InvalidArgumentException // LengthException // OutOfRangeException
Same error in other languages
Sources
Official documentation ↗
PHP Manual
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev