Over a million developers have joined DZone.

PHP 7 Errors and Exceptions

PHP 7 changes the way errors and exceptions are reported by PHP. There is a new Throwable base interface for any object that can be thrown via a throw statement. Read on and see how it works.

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

PHP 7 changes the way errors and exceptions are reported by PHP. There is a new Throwable base interface for any object that can be thrown via a throw statement.


Below you can see the complete Throwable hierarchy:

  • Throwable
    • Exception
      • LogicException
        • BadFunctionCallException
          • BadMethodCallException
        • DomainException
        • InvalidArgumentException
        • LengthException
        • OutOfRangeException
      • RuntimeException
        • OutOfBoundsException
        • OverflowException
        • RangeException
        • UnderflowException
        • UnexpectedValueException
      • ErrorException
    • Error
      • ArithmeticError
        • DivisionByZeroError
      • AssertionError
      • ParseError
      • TypeError

The Throwable interface looks like this:

Throwable {
   /* Methods */
   abstract public string getMessage ( void )
   abstract public int getCode ( void )
   abstract public string getFile ( void )
   abstract public int getLine ( void )
   abstract public array getTrace ( void )
   abstract public string getTraceAsString ( void )
   abstract public Throwable getPrevious ( void )
   abstract public string __toString ( void )

If you want to create your custom class you cannot implement the Throwable interface directly, you must extend the Exception class instead.

Below is an example of extension:

class MyCustomException extends Exception
    public function __construct($message, $code = 0, Exception $previous = null) {
        //custom code
        parent::__construct($message, $code, $previous);

    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";


The LogicException class is related to errors in the program logic that should raise a code fix.

A RuntimeException is thrown when an error is found when the program is running.

The ErrorException class is quite interesting and is mostly used to turn PHP errors in ErrorExceptions combined with set_error_handler(). The class includes a severity property for the "hardness" of the error raised.


Most of the previous fatal and recoverable fatal errors in PHP 7 are thrown an instance of Error.

An ArithmeticError is thrown for error related to mathematical operations (i.e.: wrong intdiv() calls). The DivisionByZeroError extends the ArithmeticError class and is thrown when an attempt is made to divide a number by zero.

The AssertionError class covers failures made via the assert() function.

A ParseError is thrown when parsing the eval() function causes an error or in the case of an error in the include/require functions.

TypeErrors are related to the new introduction of scalar and strict types in PHP 7 and may be thrown when the value or the type of a function argument does not match with the one declared, or an invalid number of arguments are passed (strict mode only).


If you are going to support PHP 7 be sure to catch any exception using multiple try/catch blocks. For a brand new project, you can maybe struggle to find what is the right exception to throw, but you can easily create one by yourself.

If you want to check your project quickly, you can use one of the tools below:

For more information about migration, you can use the PHP documentation.

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

php,error and exception,webdev,web

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}