SPL Exceptions
PHP's Standard Library provides a hierarchy of exception types to use for standard error conditions rather than always using generic Exception.
PHP's Standard Library provides a hierarchy of exception types to use for standard error conditions rather than always using generic Exception.
<?php
// SPL exception hierarchy under Exception:
// LogicException — programmer errors (detectable at compile-time logically)
// InvalidArgumentException
// OutOfRangeException
// BadFunctionCallException
// BadMethodCallException
// LengthException
// DomainException
// RuntimeException — runtime errors
// OutOfBoundsException
// OverflowException
// RangeException
// UnderflowException
// UnexpectedValueException
throw new InvalidArgumentException("ID must be positive");
throw new OutOfRangeException("Index 50 out of range [0,10]");
throw new RuntimeException("API returned unexpected status 503");
Use SPL exceptions for standard conditions — they signal intent immediately to other developers.