Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens with set_error_handler() on PHP7 now that all errors are exceptions?

On PHP5 it makes a whole lot of sense having both set_exception_handler() and set_error_handler() defined.

However, on PHP7 all (most?) errors are now exceptions. So, what's the point on defining both handlers, if even errors would pass by the exception handler instead?

I see there's a note on PHP7 new Error class in the exception handler doc, but there's no reference to the fact there's no plain errors anymore, but Throwables, in the error handler function.

Since PHP 7, most errors are reported by throwing Error exceptions, which will be caught by the handler as well. Both Error and Exception implements the Throwable interface. [source]

like image 518
igorsantos07 Avatar asked Nov 01 '16 13:11

igorsantos07


People also ask

How do I turn errors into exceptions in PHP?

Converting errors into exception is done by calling set_error_handler() and throw new ErrorException() in there...

In which PHP version fatal errors become exceptions?

Many fatal and recoverable fatal errors have been converted to exceptions in PHP 7. These error exceptions inherit from the Error class, which itself implements the Throwable interface (the new base interface all exceptions inherit).

Can you catch an E_error exception in PHP?

Like any other exceptions, Error objects can be caught using a try/catch block. Or you can use Throwable interface to catch all exceptions.


1 Answers

You can use php trigger_error('test error') to see what happen when error is not handled by php set_exception_handler()

like image 186
user2097418 Avatar answered Oct 05 '22 01:10

user2097418