<div dir="ltr">On Thu, Jul 11, 2013 at 5:20 AM, Mark McLoughlin <span dir="ltr"><<a href="mailto:markmc@redhat.com" target="_blank">markmc@redhat.com</a>></span> wrote:<div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
But I think what you're saying is missing is the stack trace from the<br>
underlying exception.<br>
<br>
As I understood it, Python doesn't have a way of chaining exceptions<br>
like this but e.g. Java does. A little bit more poking right now shows<br>
up this:<br>
<br>
<a href="http://www.python.org/dev/peps/pep-3134/" target="_blank">http://www.python.org/dev/peps/pep-3134/</a><br>
<br>
i.e. we can't do the right thing until Python 3, where we'd do:<br>
<br>
def download_image(host, port, path):<br>
try:<br>
s = socket.create_connection((host, port))<br>
except socket.error as e:<br>
raise ImageDownloadFailure(host, port, path, e.strerror) from e<br>
<br>
I haven't read the PEP in detail yet, though.<br><div class=""><div class="h5"><br>
</div></div></blockquote></div><div class="gmail_extra"><br></div>You can actually do this in Python 2 and keep the original context:</div><div class="gmail_extra"><br></div><div class="gmail_extra"> def download_image(host, port, path):<br>
try:<br> s = socket.create_connection((host, port))<br> except socket.error as e:</div><div class="gmail_extra"> raise ImageDownloadFailure, e, sys.exc_info()[-1]<br></div><div class="gmail_extra">
<br></div><div class="gmail_extra">This will keep the original message and stack trace, but change the type. You can also change the message if you want my mucking with e's message. I've done that to add a string like " (socket.error)" at the end of the exception message so I could see the original type.</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">If you really, really wanted to use a bare except you could also do something like:</div><div class="gmail_extra"><br></div><div class="gmail_extra" style> try:</div>
<div class="gmail_extra" style> do_something_that_raises_an_exception()</div><div class="gmail_extra" style> except:</div><div class="gmail_extra"> exc_value, exc_tb = sys.exc_info()[1:]</div><div class="gmail_extra" style>
raise MyException, exc_value, exc_tb</div><div class="gmail_extra" style><br></div><div class="gmail_extra"><div><br></div>-- <br>David<br>blog: <a href="http://www.traceback.org" target="_blank">http://www.traceback.org</a><br>
twitter: <a href="http://twitter.com/dstanek" target="_blank">http://twitter.com/dstanek</a><div>www: <a href="http://dstanek.com" target="_blank">http://dstanek.com</a></div>
</div></div>