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
Hello,
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--
--
Etienne Kneuss
http://www.colder.ch
Hi!
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
Hi!
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
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--
http://stackoverflow.com/questions/2429642/why-its-impossible-to-throw-exception-from-tostring
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu