<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: __toString() must not throw an exception</title>
	<atom:link href="http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/</link>
	<description>Phrozn, Phing &#38; Zend Framework Musings</description>
	<lastBuildDate>Tue, 15 Nov 2011 15:38:38 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: David Harkness</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-1717</link>
		<dc:creator>David Harkness</dc:creator>
		<pubDate>Wed, 02 Mar 2011 22:39:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-1717</guid>
		<description>@nairbv - PHP doesn&#039;t have checked and unchecked exceptions. There is no mechanism to declare which exceptions a method throws, and thus PHP cannot check that declared exceptions are caught.

I can see no technical reason to block __toString() from throwing exceptions.</description>
		<content:encoded><![CDATA[<p>@nairbv &#8211; PHP doesn&#8217;t have checked and unchecked exceptions. There is no mechanism to declare which exceptions a method throws, and thus PHP cannot check that declared exceptions are caught.</p>
<p>I can see no technical reason to block __toString() from throwing exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: xprt64</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-1713</link>
		<dc:creator>xprt64</dc:creator>
		<pubDate>Sun, 27 Feb 2011 09:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-1713</guid>
		<description>I agree, it&#039;s annoying. A solution could be http redirect to a error handler page, transmitting information about the error somehow (session or url parameters)</description>
		<content:encoded><![CDATA[<p>I agree, it&#8217;s annoying. A solution could be http redirect to a error handler page, transmitting information about the error somehow (session or url parameters)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: drwitt</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-1299</link>
		<dc:creator>drwitt</dc:creator>
		<pubDate>Wed, 27 Oct 2010 07:55:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-1299</guid>
		<description>In my opinion an what-ever-exception is not neccessarily equal to an user-triggered error; I usually circumvent the problem by declaring a static method somewhere that returns a custom exception output string. The catch block within __toString would then read like this: 

catch { return MyExceptionHandler::getCustomExceptionText($exception); }</description>
		<content:encoded><![CDATA[<p>In my opinion an what-ever-exception is not neccessarily equal to an user-triggered error; I usually circumvent the problem by declaring a static method somewhere that returns a custom exception output string. The catch block within __toString would then read like this: </p>
<p>catch { return MyExceptionHandler::getCustomExceptionText($exception); }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Patabugen</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-1111</link>
		<dc:creator>Patabugen</dc:creator>
		<pubDate>Mon, 19 Jul 2010 08:34:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-1111</guid>
		<description>An Excellent Solution :)
Thanks!</description>
		<content:encoded><![CDATA[<p>An Excellent Solution <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nairbv</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-1109</link>
		<dc:creator>nairbv</dc:creator>
		<pubDate>Tue, 06 Jul 2010 10:37:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-1109</guid>
		<description>I completely agree.  The problem is that PHP doesn&#039;t have a distinction between checked and unchecked (runtime) exceptions.  __toString shouldn&#039;t throw a *checked* exception but should be able to throw runtime exceptions.

Like, in Java, you can&#039;t throw a FileNotFoundException from toString.  Such an exception is a &quot;checked&quot; exception that expresses a situation that the caller should deal with.  You *can* throw a NullPointerException or ArrayIndexOutOfBoundsException from a toString method though, these are programmatic errors that should generally be propagated.

Then again, if your XML is input to the program from some external source, an exception resulting from malformed XML probably shouldn&#039;t be thrown from toString anyways, as this would be the equivalent of a checked exception (not a programmatic error).</description>
		<content:encoded><![CDATA[<p>I completely agree.  The problem is that PHP doesn&#8217;t have a distinction between checked and unchecked (runtime) exceptions.  __toString shouldn&#8217;t throw a *checked* exception but should be able to throw runtime exceptions.</p>
<p>Like, in Java, you can&#8217;t throw a FileNotFoundException from toString.  Such an exception is a &#8220;checked&#8221; exception that expresses a situation that the caller should deal with.  You *can* throw a NullPointerException or ArrayIndexOutOfBoundsException from a toString method though, these are programmatic errors that should generally be propagated.</p>
<p>Then again, if your XML is input to the program from some external source, an exception resulting from malformed XML probably shouldn&#8217;t be thrown from toString anyways, as this would be the equivalent of a checked exception (not a programmatic error).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mario Awad</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-826</link>
		<dc:creator>Mario Awad</dc:creator>
		<pubDate>Thu, 10 Dec 2009 11:56:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-826</guid>
		<description>Hi Victor,

I totally understand what you mean as this behavior annoys me. I set out trying to find a solution for this but I ran out of time and have to move on to something else. What I got so far is that Exceptions and Errors are handled differently in PHP. Maybe we can catch the error generated by __toString with one of those PHP functions:
set_error_handler
set_exception_handler

I&#039;ll keep you updated when I resume researching this issue.

On a separate note, please install the &quot;Subscribe To Comments&quot; WordPress plugin. It will allow your visitors, if they want, to receive notifications when someone posts a new comment.

Cheers :)</description>
		<content:encoded><![CDATA[<p>Hi Victor,</p>
<p>I totally understand what you mean as this behavior annoys me. I set out trying to find a solution for this but I ran out of time and have to move on to something else. What I got so far is that Exceptions and Errors are handled differently in PHP. Maybe we can catch the error generated by __toString with one of those PHP functions:<br />
set_error_handler<br />
set_exception_handler</p>
<p>I&#8217;ll keep you updated when I resume researching this issue.</p>
<p>On a separate note, please install the &#8220;Subscribe To Comments&#8221; WordPress plugin. It will allow your visitors, if they want, to receive notifications when someone posts a new comment.</p>
<p>Cheers <img src='http://www.phpmag.ru/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Victor Farazdagi</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-513</link>
		<dc:creator>Victor Farazdagi</dc:creator>
		<pubDate>Sun, 09 Aug 2009 16:46:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-513</guid>
		<description>Well, you don&#039;t seem to understand my point. I already have toXml() method, and I am not urging you to create XML in __toString(), moreover it&#039;s not about XML at all. It is all about possible complex logic in __toString() that might require exception support. 
You see if in order to overcome the deficiency of __toString, we would introduce the new methods, well, then I have great news for you - we don&#039;t need __toString&#039;s magic, long before we had toString() methods when we wanted to output our object&#039;s string representation.</description>
		<content:encoded><![CDATA[<p>Well, you don&#8217;t seem to understand my point. I already have toXml() method, and I am not urging you to create XML in __toString(), moreover it&#8217;s not about XML at all. It is all about possible complex logic in __toString() that might require exception support.<br />
You see if in order to overcome the deficiency of __toString, we would introduce the new methods, well, then I have great news for you &#8211; we don&#8217;t need __toString&#8217;s magic, long before we had toString() methods when we wanted to output our object&#8217;s string representation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: N</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-510</link>
		<dc:creator>N</dc:creator>
		<pubDate>Sun, 09 Aug 2009 15:25:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-510</guid>
		<description>If you wanna generate XML create method toXml().</description>
		<content:encoded><![CDATA[<p>If you wanna generate XML create method toXml().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Victor Farazdagi</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-506</link>
		<dc:creator>Victor Farazdagi</dc:creator>
		<pubDate>Fri, 07 Aug 2009 09:50:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-506</guid>
		<description>Well, I don&#039;t put XML generation into __toString(), I have separate method for that. __toString() just _proxies_ the XML-generating method, just to give a nice alternative of calling getXml() directly.</description>
		<content:encoded><![CDATA[<p>Well, I don&#8217;t put XML generation into __toString(), I have separate method for that. __toString() just _proxies_ the XML-generating method, just to give a nice alternative of calling getXml() directly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kso</title>
		<link>http://www.phpmag.ru/2009/08/07/__tostring-must-not-throw-an-exception/comment-page-1/#comment-505</link>
		<dc:creator>Kso</dc:creator>
		<pubDate>Fri, 07 Aug 2009 08:19:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.phpmag.ru/?p=846#comment-505</guid>
		<description>Return $e;

Exception&#039;s toString is very useful.

And don&#039;t put XML generation in toString!</description>
		<content:encoded><![CDATA[<p>Return $e;</p>
<p>Exception&#8217;s toString is very useful.</p>
<p>And don&#8217;t put XML generation in toString!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

