Skip to content

Usage: Spl Errors

Adrian edited this page Mar 25, 2024 · 9 revisions

Spl Errors

Logic Errors

These errors are programming mistakes or wrong usage (e.g., of library code) that should be identifiable during development, via static analysis, unit testing, or similar approaches. These errors should lead directly to a code fix.

  • Logic Represents a generic error in program logic.
  • BadFunctionCall Represents a function called in the wrong way, for example, providing an invalid callback or an incomplete state.
  • BadMethodCall Same as BadFunctionCall, but for instance methods.
  • Domain Used when a value does not adhere to a defined valid data domain.
  • InvalidArgument Used when an argument (such as method or function parameter) is not valid.
  • Length Represents errors caused by invalid lengths - e.g., if a string is too long or too short.
  • OutOfRange Represents an attempt to access an illegal index.

Runtime Errors

These errors are problems which arise from usage and can only be realized at runtime.

  • Runtime Represents a generic runtime error.
  • OutOfBounds Used when a value is not a valid key. This is commonly used when trying to access items in an array that do not exist.
  • Overflow Used when code tries to add an element to a full container.
  • Underflow Used when performing an invalid operation on an empty container, such as removing an element. This is the opposite of an Overflow.
  • Range Indicates range errors during program execution. Normally this means there was an arithmetic error other than under/overflow.
  • UnexpectedValue Used if a value does not match with a set of values. Typically this happens when a function calls another, and expects the return value to be of a certain type or value; not including arithmetic or buffer related errors.

Spl Exceptables

Spl Errors correspond with a set of Spl Exceptables that extend from php's built-in SPL Exception types, and are semantically equivalent:

SplError Case Exceptable Type Base Exception Type
Logic at\exceptable\Spl\LogicException LogicException
BadFunctionCall at\exceptable\Spl\BadFunctionCallException BadFunctionCallException
BadMethodCall at\exceptable\Spl\BadMethodCallException BadMethodCallException
Domain at\exceptable\Spl\DomainException DomainException
InvalidArgument at\exceptable\Spl\InvalidArgumentException InvalidArgumentException
Length at\exceptable\Spl\LengthException LengthException
OutOfRange at\exceptable\Spl\OutOfRangeException OutOfRangeException
Runtime at\exceptable\Spl\RuntimeException RuntimeException
OutOfBounds at\exceptable\Spl\OutOfBoundsException OutOfBoundsException
Overflow at\exceptable\Spl\OverflowException OverflowException
Underflow at\exceptable\Spl\UnderflowException UnderflowException
Range at\exceptable\Spl\RangeException RangeException
UnexpectedValue at\exceptable\Spl\UnexpectedValueException UnexpectedValueException

Why use Spl Exceptables?

Aside from inheriting the semantics, Spl Exceptions are standardized and built-in. Extending from them make your exceptions more understandable to more code, even if that code doesn't know what an exceptable is.