php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60188 Exception handler and shutdown hanlder were not called after throwing exception
Submitted: 2011-11-01 12:10 UTC Modified: 2011-11-02 12:12 UTC
From: hinikato at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.8 OS: Windows 7 x64
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hinikato at gmail dot com
New email:
PHP Version: OS:

 

 [2011-11-01 12:10 UTC] hinikato at gmail dot com
Description:
------------
The neither exception handler nor fatal error handler have not been called if error handler throws exception in case of including not existent file using require_once.

My PHP version is: PHP Version 5.3.8-ZS5.5.0

Test script:
---------------
<?php
namespace Foo;

class MyErrorHandler {
  function __construct() {
    set_error_handler(array($this, 'errorHandler'));
    set_exception_handler(array($this, 'exceptionHandler'));
    register_shutdown_function(array($this, 'fatalErrorHandler'));
  }

  function errorHandler() {
    echo __METHOD__ . "\n";
    throw new \Exception('test');
  }

  function exceptionHandler() {
    echo __METHOD__ . "\n";  // should be called!
  }

  function fatalErrorHandler() {
    echo __METHOD__ . "\n";  // should be called!
  }
}
echo '<pre>';
$foo = new MyErrorHandler();
require_once __DIR__ . '/not_existing_file.php';  // file should not exist
die();

Expected result:
----------------
Foo\MyErrorHandler::errorHandler
Foo\MyErrorHandler::exceptionHandler
Foo\MyErrorHandler::fatalErrorHandler

Fatal error:  main() [function.require]: Failed opening required 'X:\home\localhost\www/not_existing_file.php' (include_path='D:\system\home\projects\myak\www\includes') in X:\home\localhost\www\test.php on line 28

Actual result:
--------------
Foo\MyErrorHandler::errorHandler


Fatal error:  main() [function.require]: Failed opening required 'X:\home\localhost\www/not_existing_file.php' (include_path='D:\system\home\projects\myak\www\includes') in X:\home\localhost\www\test.php on line 28



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-11-02 12:12 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-11-02 12:12 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

If a fatal error (E_ERROR) is created the engine might be in an unreliable state, we therefore can't all error handlers. Quoting the docs:

"The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called. "
http://php.net/set_error_handler
 [2011-11-02 13:29 UTC] hinikato at gmail dot com
According this quote the bug should not occur if specified error types
will not be raised in the file where set_error_handler() is called.
Let's try to move the respective code fragment to the other file:
-- set_error_handler.php --
<?php
$foo = new \Foo\MyErrorHandler();
set_error_handler(array($foo, 'errorHandler'));
set_exception_handler(array($foo, 'exceptionHandler'));
register_shutdown_function(array($foo, 'fatalErrorHandler'));
?>
-- end of set_error_handler.php --
then let's modify slightly the our example:
-- bug --
namespace Foo;

class MyErrorHandler {
  function errorHandler() {
    echo __METHOD__ . "\n";
    throw new \Exception('test');
  }

  function exceptionHandler() {
    echo __METHOD__ . "\n";  // should be called!
  }

  function fatalErrorHandler() {
    echo __METHOD__ . "\n";  // should be called!
  }
}
require_once __DIR__ . '/set_error_handler.php';
require_once __DIR__ . '/not_existing_file.php';  // file should not
exist
die();
-- end of bug --
If you will try to run this example you will get the same invalid
behavior.
 [2011-11-02 13:35 UTC] hinikato at gmail dot com
Why PHP will not call at least shutdown function in such situtations? Because the engine is in the an unreliable state... What is unreliable state? Is it bug that PHP developers unable to fix due some limitations?
 [2011-11-02 21:25 UTC] hinikato at gmail dot com
And please note that the bug causes the following handlers will not be called:
1. array($foo, 'exceptionHandler');
2. array($foo, 'fatalErrorHandler');
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 20:01:32 2025 UTC