Req #72089 [Com]: handle all fatal errors with try catch
| From: | spam2 at rhsoft dot net | Date: | Wed, 07 Jun 2017 09:08:46 +0000 |
| Subject: | Req #72089 [Com]: handle all fatal errors with try catch | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209401@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=72089&edit=1
ID: 72089
Comment by: spam2 at rhsoft dot net
Reported by: paulobitfranca at gmail dot com
Summary: handle all fatal errors with try catch
Status: Re-Opened
Type: Feature/Change Request
Package: *General Issues
Operating System: Linux
PHP Version: 7.0.5
Block user comment: N
Private report: N
New Comment:
the point is with PHP7 should *anything* which is not catchable considered and handeled as bug
Previous Comments:
------------------------------------------------------------------------
[2017-06-07 09:05:30] mplomer at gmx dot de
> "if a file is *required* and it can't be included, it doesn't make much sense to
> go on."
You can say the same for a ParseError ;-)
I am currently designing a simple router where it is possible that a PHP file does not exist (to
avoid a central configuration), and I want to throw a 404 then. I was happy to handle this with the
new PHP 7 exception concept, as this was my first use case for it ... but then I also noticed, it
just does not work for non-existent files :-(
IMHO it is really inconsistent to throw errors for parse-errors but then generate fatal errors for
non-existent files.
------------------------------------------------------------------------
[2017-04-04 07:13:13] requinix@php.net
Related To: Bug #74366
------------------------------------------------------------------------
[2016-06-17 07:57:54] lisachenko dot it at gmail dot com
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
}
------------------------------------------------------------------------
[2016-04-26 17:50:26] cmb@php.net
Generally, that has already been done for PHP 7.0.0 where
reasonable and possible, see
<https://wiki.php.net/rfc/engine_exceptions_for_php7>.
With regard to your example: if a file is *required* and it can't
be included, it doesn't make much sense to go on. If on the other
hand the file is not really required, just
include it.
------------------------------------------------------------------------
[2016-04-23 21:28:00] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72089&edit=1