[Python-ideas] why not try without except?

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Apr 26 13:38:07 CEST 2009


spir wrote:

>    option self.fill(self.fillColor)
> can only catch AttributeError for 'fillColor' or for 'fill'.

You mean that 'option' *only* catches AttributeError?

What makes you think that AttributeError is the most
common exception to want to catch in these kinds of
situations? Seems to me you're just as likely to
want to catch one of ValueError, IndexError, KeyError,
etc.

Also, I don't find your fillColor example very
convincing. If I have a class that may or may not
have a fillColor, I set the fillColor to None when
it doesn't have one -- I don't just leave the
fillColor attribute undefined. The code then
becomes

   if self.fillColor:
     self.fill(self.fillColor)

or if I want to minimise attribute lookups,

   color = self.fillColor
   if color:
     self.fill(color)

-- 
Greg



More information about the Python-ideas mailing list