FAQ
Hi.

Given the relatively simple code below, why can't I throw an exception
in __toString()?

<?php
class AlwaysFails {
public function asString() {
throw new Exception('Failed in asString()');
}

public function __toString() {
throw new Exception('Failed in __toString()');
}
}

$failure = new AlwaysFails;
try {
echo $failure->asString();
}
catch(Exception $ex) {
echo $ex->getMessage();
}

try {
echo $failure;
}
catch(Exception $ex) {
echo $ex->getMessage();
}
?>

outputs ...

Failed in asString()
Fatal error: Method AlwaysFails::__toString() must not throw an
exception in Z:\fa1.php on line 21




--
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

Search Discussions

  • Etienne Kneuss at Aug 2, 2011 at 2:51 pm
    Hello,
    On Tue, Aug 2, 2011 at 16:47, Richard Quadling wrote:
    Hi.

    Given the relatively simple code below, why can't I throw an exception
    in __toString()?
    The (historic) reason is that the toString conversion occurs at
    various places in the engine, and in some of them, throwing exceptions
    caused trouble. It is not clear whether this limitation it still
    required. In any case, it could almost certainly be fixed.

    Best,
    <?php
    class AlwaysFails {
    public function asString() {
    throw new Exception('Failed in asString()');
    }

    public function __toString() {
    throw new Exception('Failed in __toString()');
    }
    }

    $failure = new AlwaysFails;
    try {
    echo $failure->asString();
    }
    catch(Exception $ex) {
    echo $ex->getMessage();
    }

    try {
    echo $failure;
    }
    catch(Exception $ex) {
    echo $ex->getMessage();
    }
    ?>

    outputs ...

    Failed in asString()
    Fatal error: Method AlwaysFails::__toString() must not throw an
    exception in Z:\fa1.php on line 21




    --
    Richard Quadling
    Twitter : EE : Zend : PHPDoc
    @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php


    --
    Etienne Kneuss
    http://www.colder.ch
  • Richard Quadling at Aug 2, 2011 at 3:56 pm

    On 2 August 2011 16:09, Keloran wrote:
    i didnt think any of the magic functions could throw exceptions
    All magic methods can throw exceptions except __toString.

    There is some oddity with __set_state() (in terms of the pattern of
    output is different).

    http://pastebin.com/nGkP7kKf

    outputs ...

    In : failure2::__construct
    Thrown exception in : failure2::__construct

    In : failure1::__get
    Thrown exception in : failure1::__get

    In : failure1::__set
    Thrown exception in : failure1::__set

    In : failure1::__call
    Thrown exception in : failure1::__call

    In : failure1::__callStatic
    Thrown exception in : failure1::__callStatic

    In : failure1::__isset
    Thrown exception in : failure1::__isset

    In : failure1::__unset
    Thrown exception in : failure1::__unset

    In : failure1::__invoke
    Thrown exception in : failure1::__invoke

    In : failure1::__clone
    Thrown exception in : failure1::__clone
    DateTime::__set_state(array(
    'date' => '2011-08-02 16:54:03',
    'timezone_type' => 3,
    'timezone' => 'Europe/London',
    ))
    In : failure3::__destruct
    Thrown exception in : failure3::__destruct

    In : failure1::__sleep
    Thrown exception in : failure1::__sleep

    In : failure1::__wakeup
    Thrown exception in : failure1::__wakeup


    But __toString() throws a Fatal Error.



    --
    Richard Quadling
    Twitter : EE : Zend : PHPDoc
    @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea
  • Stas Malyshev at Aug 2, 2011 at 11:52 pm
    Hi!
    On 8/2/11 7:51 AM, Etienne Kneuss wrote:
    The (historic) reason is that the toString conversion occurs at
    various places in the engine, and in some of them, throwing exceptions
    caused trouble. It is not clear whether this limitation it still
    required. In any case, it could almost certainly be fixed.
    In theory, yes. In practice, that means ensuring every place in the
    engine that does convert_to_string() can be safely interrupted by an
    exception and can handle that exception properly. This is pretty hard to
    do.
    --
    Stanislav Malyshev, Software Architect
    SugarCRM: http://www.sugarcrm.com/
    (408)454-6900 ext. 227
  • Richard Quadling at Aug 6, 2011 at 9:30 am

    On 3 August 2011 00:51, Stas Malyshev wrote:
    Hi!
    On 8/2/11 7:51 AM, Etienne Kneuss wrote:

    The (historic) reason is that the toString conversion occurs at
    various places in the engine, and in some of them, throwing exceptions
    caused trouble. It is not clear whether this limitation it still
    required. In any case, it could almost certainly be fixed.
    In theory, yes. In practice, that means ensuring every place in the engine
    that does convert_to_string() can be safely interrupted by an exception and
    can handle that exception properly. This is pretty hard to do.
    --
    Stanislav Malyshev, Software Architect
    SugarCRM: http://www.sugarcrm.com/
    (408)454-6900 ext. 227
    What would be a good way to explain this in the documentation. Just
    saying that you can't is only going to present more questions.
    Explaining it a way that isn't too technical, but covers the basis
    would be good.



    --
    Richard Quadling
    Twitter : EE : Zend : PHPDoc
    @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea
  • Ferenc Kovacs at Aug 2, 2011 at 4:00 pm

    On Tue, Aug 2, 2011 at 4:47 PM, Richard Quadling wrote:
    Hi.

    Given the relatively simple code below, why can't I throw an exception
    in __toString()?

    <?php
    class AlwaysFails {
    public function asString() {
    throw new Exception('Failed in asString()');
    }

    public function __toString() {
    throw new Exception('Failed in __toString()');
    }
    }

    $failure = new AlwaysFails;
    try {
    echo $failure->asString();
    }
    catch(Exception $ex) {
    echo $ex->getMessage();
    }

    try {
    echo $failure;
    }
    catch(Exception $ex) {
    echo $ex->getMessage();
    }
    ?>

    outputs ...

    Failed in asString()
    Fatal error: Method AlwaysFails::__toString() must not throw an
    exception in Z:\fa1.php on line 21




    --
    Richard Quadling
    Twitter : EE : Zend : PHPDoc
    @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

    --
    PHP Internals - PHP Runtime Development Mailing List
    To unsubscribe, visit: http://www.php.net/unsub.php
    http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exception-from-tostring


    --
    Ferenc Kovács
    @Tyr43l - http://tyrael.hu

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupphp-internals @
categoriesphp
postedAug 2, '11 at 2:48p
activeAug 6, '11 at 9:30a
posts6
users4
websitephp.net

People

Translate

site design / logo © 2022 Grokbase