|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-11-20 16:55 UTC] danack@php.net
-Status: Open
+Status: Not a bug
[2020-11-20 16:55 UTC] danack@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 22:00:02 2025 UTC |
Description: ------------ I encountered an issue with code in a namespaced file and strict_types=1 where i forgot to include either a use statement for \Exception or indicate I wanted to catch an Exception in the default namespace. I expected this code to trigger a warning or raise a TypeError as the implied type did not exist. However, it appears that the type in a catch statement is not validated. In my example, the catch statement tries to catch a Bug\Exception, however this class does not exist and does not match the \Exception which is thrown. The thrown \Exception is therefore reported back. I believe that PHP developers would benefit from PHP adding validation of the type to the catch statement at least in some configuration(s) eg strict_types=1 Test script: --------------- <?php /** * The type in a catch statement is not validated even with strict_type=1 * * @author Tom Egan */ declare(strict_types=1); namespace Bug; try { throw new \Exception('A test exception'); } catch(Exception $e) { echo $e->getMessage(); } exit; Expected result: ---------------- $ php bug-test.php PHP Fatal error: Uncaught TypeError: the type Bug\Exception does not exist bug-test.php:15 Stack trace: #0 {main} thrown in bug-test.php on line 15 Actual result: -------------- $ php bug-test.php PHP Fatal error: Uncaught Exception: A test exception in bug-test.php:14 Stack trace: #0 {main} thrown in bug-test.php on line 14