php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65255 exception not catchable when thrown from autoload in an extended class
Submitted: 2013-07-12 19:43 UTC Modified: 2013-07-14 03:17 UTC
From: ryan dot brothers at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.0 OS: Linux
Private report: No CVE-ID:
 [2013-07-12 19:43 UTC] ryan dot brothers at gmail dot com
Description:
------------
This issue may be the same issue as bug 65254 that I just submitted.  The following script throws a Fatal Error rather than catching the exception.


Test script:
---------------
<?php
function __autoload($class)
{
    throw new Exception('abcd');
}

class test1 extends test2
{
    public static function go()
    {

    }
}

try
{
    test1::go();
}
catch (Exception $e)
{
    echo 'caught';
    exit;
}

Expected result:
----------------
caught

Actual result:
--------------
Fatal error: Uncaught exception 'Exception' with message 'abcd' in /tmp/test.php:4
Stack trace:
#0 /tmp/test.php(8): __autoload('test2')
#1 {main}
  thrown in /tmp/test.php on line 4


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-14 03:17 UTC] laruence@php.net
-Status: Open +Status: Not a bug
 [2013-07-14 03:17 UTC] laruence@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

try the following example, you may found why this is expected:
<?php
function __autoload($class)
{
    throw new Exception('abcd');
}

try
{
    class test1 extends test2
    {
        public static function go()
        {

        }
    }
    test1::go();
}
catch (Exception $e)
{
    echo 'caught';
    exit;
}
 [2013-07-14 13:22 UTC] ryan dot brothers at gmail dot com
Thank you for your help.  Why does it matter if the class was declared literally in the try/catch block?  The documentation states "exceptions thrown in the __autoload function can be caught in the catch block".  It works for the base class, but not extended classes.

Other exceptions thrown outside of a try/catch always propagate back to the caller's try/catch such as the below.  In these examples, the exception is caught, but the exception is thrown outside of the literal try/catch.

<?php
function __autoload($class)
{
    throw new Exception('abcd');
}

try
{
    test1::go();
}
catch (Exception $e)
{
    echo 'caught';
    exit;
}

=================================================

<?php
function test()
{
     throw new Exception('abcd');
}

try
{
    test();
}
catch (Exception $e)
{
    echo 'caught';
    exit;
}

=================================================

<?php
class test1
{
    public static function go()
    {
        throw new Exception('abcd');
    }
}

try
{
    test1::go();
}
catch (Exception $e)
{
    echo 'caught';
    exit;
}
 [2013-07-14 13:41 UTC] ryan dot brothers at gmail dot com
Nevermind, I see the issue now and why it's expected behavior.  The autoload gets called when the class is declared rather than when the class is first called.  I'll see if your fix in bug 65254 resolves the issue I was having.  Thanks again for your help.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 06:02:13 2014 UTC