<?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: Python Exception Handling: Cleanup and Reraise</title>
	<atom:link href="http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise</link>
	<description>Software Development, version 3.0</description>
	<lastBuildDate>Fri, 05 Mar 2010 15:01:53 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Brian St. Pierre</title>
		<link>http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise/comment-page-1#comment-4975</link>
		<dc:creator>Brian St. Pierre</dc:creator>
		<pubDate>Tue, 10 Nov 2009 03:32:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise#comment-4975</guid>
		<description>&lt;a href=&quot;http://posterous.com/people/4aGe7xpRQXVn&quot; rel=&quot;nofollow&quot;&gt;Haridara&lt;/a&gt; left a comment on my (now abandoned -- a short lived experiment) posterous version of this blog.

I am quoting here because it includes a helpful link and useful tips:

&lt;blockquote&gt;
Also see &lt;a href=&quot;http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/&quot; rel=&quot;nofollow&quot;&gt;http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/&lt;/a&gt; for a good discussion on the different scenarios.

Regarding reraiser5(), See the specific discussion in the comments on this pattern. Couple of comments I have are:
- You wouldn&#039;t want to just &quot;pass&quot;, you should at least the exception, e.g., traceback.print_exc()
- Sometimes you want to cleanup conditionally, only if the operation fails.
- During cleanup, it is better to be more broader in catching exceptions.

Here is an example that I posted at the other blog as well:

&lt;pre&gt;
success = 0
try:
    do_db_operation()
    success = 1
finally:
    if not success:
        try:
            do_rollback()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            logging.getLogger().exception(&quot;Exception in cleanup ignored due to a pending exception on stack&quot;)
    else:
        do_commit()
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p><a href="http://posterous.com/people/4aGe7xpRQXVn" rel="nofollow">Haridara</a> left a comment on my (now abandoned &#8212; a short lived experiment) posterous version of this blog.</p>
<p>I am quoting here because it includes a helpful link and useful tips:</p>
<blockquote><p>
Also see <a href="http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/" rel="nofollow">http://blog.ianbicking.org/2007/09/12/re-raising-exceptions/</a> for a good discussion on the different scenarios.</p>
<p>Regarding reraiser5(), See the specific discussion in the comments on this pattern. Couple of comments I have are:<br />
- You wouldn&#8217;t want to just &#8220;pass&#8221;, you should at least the exception, e.g., traceback.print_exc()<br />
- Sometimes you want to cleanup conditionally, only if the operation fails.<br />
- During cleanup, it is better to be more broader in catching exceptions.</p>
<p>Here is an example that I posted at the other blog as well:</p>
<pre>
success = 0
try:
    do_db_operation()
    success = 1
finally:
    if not success:
        try:
            do_rollback()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            logging.getLogger().exception("Exception in cleanup ignored due to a pending exception on stack")
    else:
        do_commit()
</pre>
</blockquote>
]]></content:encoded>
	</item>
	<item>
		<title>By: kevin clark</title>
		<link>http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise/comment-page-1#comment-3606</link>
		<dc:creator>kevin clark</dc:creator>
		<pubDate>Fri, 17 Jul 2009 14:45:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.bstpierre.org/python-exception-handling-cleanup-and-reraise#comment-3606</guid>
		<description>I guess the point of my comment is to affirm your thinking here:  I hadn&#039;t done a lot of coding with exception handling before this past year, but for a project that I was working on (in Java) I eventually arrived at the same pattern that you did here:  using &quot;finally&quot; is the way to go, in my opinion.  It is refreshing to arrive at a clean code pattern for problems like this, in contrast to  some of the messier solutions (or, in fact, buggy code...) that I have seen elsewhere...</description>
		<content:encoded><![CDATA[<p>I guess the point of my comment is to affirm your thinking here:  I hadn&#8217;t done a lot of coding with exception handling before this past year, but for a project that I was working on (in Java) I eventually arrived at the same pattern that you did here:  using &#8220;finally&#8221; is the way to go, in my opinion.  It is refreshing to arrive at a clean code pattern for problems like this, in contrast to  some of the messier solutions (or, in fact, buggy code&#8230;) that I have seen elsewhere&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
