Throwing exception within exception handler

后端 未结 5 1171
温柔的废话
温柔的废话 2021-02-19 18:44

I have a script with an exception handler. This exception handler cleans up a couple connections, prior to the script exiting after an exception.

I would like to re-thr

5条回答
  •  忘了有多久
    2021-02-19 19:30

    You can not re-throw from the exception handler, however, there are other places you can. For example you can de-couple the re-throw from the handler by encapsulating things into a class of it's own and then use the __destruct() function (PHP 5.3, Demo):

    rethrow = $exception;
        }
        public function __destruct()
        {
            if ($this->rethrow) throw $this->rethrow;
        }
    }
    
    $handler = new ExceptionHandler;
    
    throw new Exception();
    

    Put this into my error log:

    [29-Oct-2011 xx:32:25] PHP Fatal error: Uncaught exception 'Exception' in /.../test-exception.php:23
    Stack trace:
    #0 {main}
    thrown in /.../test-exception.php on line 23
    

提交回复
热议问题