php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70844 spl_autoload_register throws expected exception too early
Submitted: 2015-11-03 14:47 UTC Modified: 2016-03-18 19:46 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: mjauram at gmail dot com Assigned: nikic (profile)
Status: Closed Package: SPL related
PHP Version: 5.5.30 OS: Windows/Linux
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: mjauram at gmail dot com
New email:
PHP Version: OS:

 

 [2015-11-03 14:47 UTC] mjauram at gmail dot com
Description:
------------
If loading classes dynamically inside of the finally block's try-catch block then auto load throws previously throwed exception which is catched.

Test script:
---------------
Full example here: https://goo.gl/08KRD2

try {
    throw new \Exception('Exception');
} catch (\Exception $e) {
    throw new \Exception('New Exception');
} finally {
    try{
        myclass2::test(); // spl_autoload_register function to load classes
    } catch (\Exception $ex) {
        var_dump('this is not expected:', $ex); // this is not expected but "New Exception" is caught here
    }
}

Expected result:
----------------
New Exception is thrown not catched.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-14 13:26 UTC] andreimarchenko90 at gmail dot com
The same situation with this bug:
<?php
spl_autoload_register(function ($class_name) {
    echo "Need to include `{$class_name}` \n";
});

try {
    try {
        throw new \RuntimeException('Error message');
    } finally {
        echo 'Start finally block' . PHP_EOL;
        $foo = new Foo();
        echo 'End finally block' . PHP_EOL;
    }
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL;
    echo $e . PHP_EOL;
}


Actual result:

Start finally block
Need to include `Foo` 
Error message
exception 'RuntimeException' with message 'Error message' in /in/Bn1d8:8
Stack trace:
#0 {main}

Expected result:

Start finally block
Need to include `Foo` 

Fatal error: Uncaught RuntimeException: Error message in /in/Bn1d8:8
Stack trace:
#0 {main}

Next Error: Class 'Foo' not found in /in/Bn1d8:11
Stack trace:
#0 {main}
  thrown in /in/Bn1d8 on line 11

Process exited with code 255.
 [2016-03-18 19:46 UTC] nikic@php.net
Closing here, as both examples seem to be working fine now: https://3v4l.org/TAkta and https://3v4l.org/Bn1d8. On 5.6 you get the fatal error from class loading, on 7.0 chained exceptions.
 [2016-03-18 19:46 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC