Handling errors and exceptions in php7

Recently ported to php7. The following error has occurred:

argument 1 passed to MyClass\Throwable::exceptionHandler() must be an instance of Exception, instance of Error given

And the corresponding class

namespace MyClass;

class Throwable
{
    public function exceptionHandler(\Exception $exception)
    {
        //logic here
    }
}

As stated in the docs

now most errors are logged by throwing exception exceptions.

Does this mean that I have to provide an instance Erroror even more general Throwablefor the exception handler?

+4
source share
1 answer

Errorsand Exceptionsexpand Throwable, however errors do not extend from Exception.

Therefore, your ExceptionHandler must accept a Type object Throwablein order to accept Errors.

- , $exception, .

namespace MyClass;

class Throwable
{
    public function exceptionHandler(\Throwable $exception)
    {
        //logic here
    }
}

. Error ErrorException, PHP 5 Exception .

http://php.net/manual/en/class.error.php

+2

Source: https://habr.com/ru/post/1627751/


All Articles