PHP 7 try - catch: unable to catch “Catchable fatal error”

后端 未结 3 1575
青春惊慌失措
青春惊慌失措 2020-12-11 17:23

I am playing with try - catch block:



        
3条回答
  •  抹茶落季
    2020-12-11 18:03

    There might be some fatal errors which are not even caught by set_error_handler() or \Throwable.

    The below implementation will catch the errors which are not even caught by \Throwable as tested in php 7.1. It should only be implemented in your development environment(by just adding it in your development config file) and shouldn't be done in production.

    Implementation

    register_shutdown_function(function () {
        $err = error_get_last();
        if (! is_null($err)) {
            print 'Error#'.$err['message'].'
    '; print 'Line#'.$err['line'].'
    '; print 'File#'.$err['file'].'
    '; } });

    Example Error

    Error# Class Path/To/MyService contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Path/To/MyServiceInterface::add)
    Line# 12
    File# Path/To/MyService.php
    

提交回复
热议问题