Bug #65255 [Com]: exception not catchable when thrown from autoload in an extended class

From: Date: Sun, 14 Jul 2013 13:41:09 +0000
Subject: Bug #65255 [Com]: exception not catchable when thrown from autoload in an extended class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-180258@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65255&edit=1

 ID:                 65255
 Comment by:         ryan dot brothers at gmail dot com
 Reported by:        ryan dot brothers at gmail dot com
 Summary:            exception not catchable when thrown from autoload in
                     an extended class
 Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.5.0
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2013-07-14 13:22:01] 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 03:17:40] 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-12 19:43:37] 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



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65255&edit=1


Thread (4 messages)

« previous php.bugs (#180258) next »