|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-04-23 21:28 UTC] paulobitfranca at gmail dot com
Description:
------------
I mind that some fatal errors are not handled by the try/catch yet.
For example, when I use require() and the include file doesn't exist.
Is it possible that in a future version, all the fatal erros can be handled by try/catch?
Test script:
---------------
<?php
error_reporting(E_ALL);
try
{
require("xxx.php");
}
catch (Throwable $e)
{
echo "An error: file doesn't exist"
}
echo "hi!";
Expected result:
----------------
An error: file doesn't exist
Hi!
Actual result:
--------------
Fatal error: require() [function.require.html: Failed opening required 'xxx.php' (include_path='.:/opt/php7/includes') in /var/www/html/teste.php on line 8
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 17:00:01 2025 UTC |
I think, that we should replace all possible places with Fatal Errors with correct throwing of Error. This will give us more control over the file loading process and make it more atomic, because additional checks if(file_exists($file) && is_readable($file)) generate extra stat commands and slow. Moreover, for highload project, we can hit a situation where if succeeded, but require will fail because file was deleted after check. Code like this: try { require $fileName; } catch (Error $e) { echo "Oops, " . $e; } is much more reliable than this one: if (file_exists($fileName) && is_readable($fileName)) { @include $fileName; // Silencing errors for the case of race-condition, etc }In the same train of thought, why does it make sense that running foo.php, with these contents: ---------- foo.php <?php include "bar.php"; ---------- and ---------- bar.php <?php asdfkoaw jeokjwe foijsdfklojsefwe ---------- Would throw a 'ParseError' exception that can be caught and handled, but when I replace the content of bar.php with the much more correct code: ---------- bar.php <?php class A implements B {} ---------- Gives me a unrecoverable fatal error: Interface 'B' not found. So a very big +1000 on making all failures in requiring files, and basically ALL errors, throw exceptions that can be caught. Even if the script can't continue, it can do its best to provide a meaningful or at least thoughtful error message to the client.it's a general issue - with having Üeverything* as exception you can esily write code which handles 99.9% of all cases in a very cheap try{] while handle servers missing something in the catch part - unhandeled you get the same result as now but *you can* handle it properlyAs an actual case where this issue is a valid concern. Pseudocode that illustrates the issue: <?php try { SoapClient::__constructor($wsdl) } catch (\Throwable $t) { // Never executed because ANYTIME the constructor can not PARSE the WSDL a script exiting fatal is thrown and not caught! // The SOAP implementation in PHP offers ZERO fault tolerance. // We can't degrade gracefully. // All processing stops and the script is exited! // Why in the name of sanity is a SOAP PARSE issue NOT throwing a SOAPFault and instead we get a fatal error? }