How to except the unexpected?

Roy Smith roy at panix.com
Fri Mar 3 19:46:22 EST 2006


In article <4408db38$0$21898$5a62ac22 at per-qv1-newsreader-01.iinet.net.au>,
 Ben Caradoc-Davies <ben at wintersun.org> wrote:

> James Stroud wrote:
> > except URLError, HTTPException:
> 
> Aieee! This catches only URLError and binds the name HTTPException to 
> the detail of that error. You must write
> 
> except (URLError, HTTPException):
> 
> to catch both.

This exact issue came up just within the past week or so.  I think that 
qualifies it as a wart, but I think it's a double wart.

It's certainly a wart that the try statement syntax allows for such 
ambiguity.  But, I think it's also a wart in how the exceptions were 
defined.  I like to create a top-level exception class to encompass all the 
possible errors in a given module, then subclass that.  This way, if you 
want to catch anything to goes wrong in a call, you can catch the top-level 
exception class without having to enumerate them all.



More information about the Python-list mailing list