php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36896 ErrorException does not work with include/require errors
Submitted: 2006-03-28 17:51 UTC Modified: 2006-03-28 19:06 UTC
From: akorthaus at web dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.2 OS: Linux 2.4.32 (gentoo)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: akorthaus at web dot de
New email:
PHP Version: OS:

 

 [2006-03-28 17:51 UTC] akorthaus at web dot de
Description:
------------
The ErrorException class bundled with PHP 5.1 does not work with Warnings/Fatal Errors produced by include() and require().

Reproduce code:
---------------
<?php
function ErrorsToExceptions($severity, $message) {
  throw new ErrorException($message, 0, $severity);
}
set_error_handler('ErrorsToExceptions');

try {
  include('./DOES_NOT_EXIST');
}
catch (Exception $e) {
  echo get_class($e) . " catched!\n"
  echo $e->getMessage();
}
?>


Expected result:
----------------
should throw an ErrorException and display $e->getMessage(), and should NOT display a PHP Warning.

Actual result:
--------------
Warning: main(): Failed opening './DOES_NOT_EXIST' for inclusion (include_path='.:') in /home/akorthaus/test/exc4.php on line 16
ErrorException catched!
include(./DOES_NOT_EXIST): failed to open stream: No such file or directory

if I replace "include" with "require", I get:

Fatal error: main(): Failed opening required './DOES_NOT_EXIST' (include_path='.:') in /home/akorthaus/test/exc4.php on line 14

if I replace "require" with "file_get_contents", I get:

ErrorException catched!

file_get_contents(./DOES_NOT_EXIST): failed to open stream: No such file or directory

That's what I'd expect to see from include() and require() too (ErrorException also works with other Fatal Errors!). 

So include() seems to throw an ErrorException, but additionaly displays the PHP Warning, require only displays the PHP Fatal Error, and does not throw an Exception at all. 

Is this intended? Is it somewhere documented how ErrorException works or should be used?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-28 18:00 UTC] tony2001@php.net
Fixed in CVS yesterday.
 [2006-03-28 19:06 UTC] akorthaus at web dot de
I testet the latest 5.1-dev (5.1.3RC2-dev) snapshot now and everything works fine now - thanks a lot!

Is there any documentation about ErrorException class?

Is

function ErrorsToExceptions($severity, $message) {
  throw new ErrorException($message, 0, $severity);
}
set_error_handler('ErrorsToExceptions');

correct / the recommended way?

Is there a plan for an engine-level function to replace the code above, something like "use_exception_error_handler()"?
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 07:01:33 2025 UTC