Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Exceptions and Errors

The PHP Error class

Reference for the PHP Error class: the base of engine errors such as TypeError and DivisionByZeroError, how it differs from Exception and how to catch it.

Error is the base class for problems that the PHP engine itself reports: calling an undefined function, passing the wrong type to a typed parameter, dividing by zero with intdiv(), reading a property of null in a way that cannot work, and so on. Before PHP 7 most of these were fatal errors that ended the script; since PHP 7 they are thrown objects you can catch.

Error has exactly the same methods as Exception and also implements Throwable, but the two classes are siblings, not parent and child. That is deliberate: a catch (Exception $e) written for your own business-logic failures will not accidentally swallow programming mistakes.

PHP
try {
    undefined_function();
} catch (Error $e) {
    echo $e->getMessage(); // Call to undefined function undefined_function()
}

Try it

PHP Common engine errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Error versus Exception

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
class Error implements Throwable
{
    public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null);
    // getMessage(), getCode(), getPrevious(), getFile(), getLine(),
    // getTrace(), getTraceAsString(), __toString()
}
Name Description
$message The text describing the problem.
$code An integer code, 0 for engine errors.
$previous The error or exception that led to this one.

Direct subclasses that the engine throws: TypeError (and its child ArgumentCountError), ValueError, ArithmeticError (and its child DivisionByZeroError), AssertionError, UnhandledMatchError and CompileError (with ParseError).

Return value (methods): identical to Exception.

Usage notes

  • Don't catch Error to hide bugs. An undefined function or wrong argument type is a mistake in the code. Fix it; catch Error only at the outer edge of the program to log it and show a friendly page.
  • Exception does not catch it. Use catch (Error $e) or catch (Throwable $e) as in the second example. Code written for PHP 5 that catches only Exception misses many failures.
  • Uncaught errors are fatal. The script stops with "Fatal error: Uncaught Error: ..." and a stack trace (the last lines of the second example show it), unless set_exception_handler() handles it.
  • Warnings and notices are different. They are not objects. Undefined variables, array keys and similar issues produce warnings that you can convert into exceptions with set_error_handler().
  • ParseError is thrown by eval() and include when the code has a syntax error, so you can catch a bad template; a syntax error in the main script is still fatal.
  • Throwing it yourself is rare. For your own failures, throw an Exception subclass so callers can tell your errors from engine errors.
  • Messages are not a stable API. Match on the class (TypeError), not on the message text, which can change between PHP versions.