Hello,

On Tue, Aug 2, 2011 at 16:47, Richard Quadling <rquadl...@gmail.com> 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

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

Reply via email to