php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69678 Exception handling consistency with namespace
Submitted: 2015-05-21 07:14 UTC Modified: 2015-05-21 09:51 UTC
From: pahanmipt at mail dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.6.9 OS: Ubuntu 14.04
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: pahanmipt at mail dot ru
New email:
PHP Version: OS:

 

 [2015-05-21 07:14 UTC] pahanmipt at mail dot ru
Description:
------------
The exception from global namespace is not catched at namespaced class methods if exception name is not fully qualified at catch block and no error occured.



Test script:
---------------
https://github.com/0x-ff/php-gotchas/blob/master/intersting_exception_handling.php

Expected result:
----------------
I think that it would be a fatal error to consistent behaviour with fatal at
throw new GlobalException(); // not fully qualified => not found
catch(GlobalException $e) {  // not fully qualified => MUST BE FATAL not found


Actual result:
--------------
The exception class GlobalException at test script catches at class only if 
fully qualified

namespace ...;

try {
throw new \GlobalException("");
} catch (GlobalException $e) { // Not catched here and no error occured that its not fully qualified, just silently ignored that exception

}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-21 08:29 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2015-05-21 08:29 UTC] nikic@php.net
We don't require the exception class to exist in the catch() clause, because it might be loaded at a later time or different configuration. A simple example of why this is useful is this:

    try {
        // ...
    } catch (Exception $e) {
        // ...
    } catch (BaseException $e) {
        // ...
    }

This allows you to catch all exceptions on both PHP 5 and 7. You will not get an error on PHP 5 because "BaseException" does not exist there.
 [2015-05-21 09:51 UTC] pahanmipt at mail dot ru
Ok. Could we require it when the exception throwed at try block ?
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 13:00:01 2026 UTC