PHP 7.2.0 Beta 3 Released

Errors in PHP 7

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing Error exceptions.

As with normal exceptions, these Error exceptions will bubble up until they reach the first matching catch block. If there are no matching blocks, then any default exception handler installed with set_exception_handler() will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a traditional error.

As the Error hierarchy does not inherit from Exception, code that uses catch (Exception $e) { ... } blocks to handle uncaught exceptions in PHP 5 will find that these Errors are not caught by these blocks. Either a catch (Error $e) { ... } block or a set_exception_handler() handler is required.

add a note add a note

User Contributed Notes 6 notes

hungry dot rahly at gmail dot com
1 year ago
You can catch both exceptions and errors by catching(Throwable)
demis dot palma at tiscali dot it
1 year ago
Throwable does not work on PHP 5.x.

To catch both exceptions and errors in PHP 5.x and 7, add a catch block for Exception AFTER catching Throwable first.
Once PHP 5.x support is no longer needed, the block catching Exception can be removed.

   // Code that may throw an Exception or Error.
catch (Throwable $t)
   // Executed only in PHP 7, will not match in PHP 5
catch (Exception $e)
   // Executed only in PHP 5, will not be reached in PHP 7
lubaev dot ka at gmail dot com
8 months ago
php 7.1

try {
   // Code that may throw an Exception or ArithmeticError.
} catch (ArithmeticError | Exception $e) {
   // pass
ryan dot jentzsch@{gmail} dot com
12 days ago
An excellent blog post on the difference between exceptions, throwables and how PHP 7 handles these can be found here:
1 month ago
Maybe you want to suppress warnings in errlog? If so, then just prepend function name with @ sign.
Egor Gubanov
5 months ago
What should I do to let the program to react to warnings? I've already tried to write try/catch construction, but it doesn't working.
To Top