SyntaxStudy
Sign Up
PHP Intermediate 4 min read

SPL Exception Classes

SPL Exceptions

PHP's Standard Library provides a hierarchy of exception types to use for standard error conditions rather than always using generic Exception.

Example
<?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");
Pro Tip

Use SPL exceptions for standard conditions — they signal intent immediately to other developers.