How to except the unexpected?

Roy Smith roy at panix.com
Sat Mar 4 15:31:34 CET 2006


In article <8n3j02ld5rohugr381n2k2pjec4em24f2g at 4ax.com>,
 Rene Pijlman <reply.in.the.newsgroup at my.address.is.invalid> wrote:

> Roy Smith:
> >In theory, all exceptions which represent problems with the external 
> >environment (rather than programming mistakes) should derive from 
> >Exception, but not from StandardError.
> 
> Are you sure?
> 
> """
> The class hierarchy for built-in exceptions is:
> 
>     Exception
>      +-- StandardError
>      |    +-- KeyboardInterrupt
>      |    +-- ImportError
>      |    +-- EnvironmentError
>      |    |    +-- IOError
> """
> http://www.python.org/doc/current/lib/module-exceptions.html

Hmmm, OK, I missed EnvironmentError.  So, what you need to do is:

try:
   whatever()
except EnvironmentError:
   ...
except StandardError:
   ...
except Exception:
   ...

or something like that.

I do agree with you that there is some value in Java's "must catch or 
re-export all exceptions" semantics, and this would be one of those places 
where it would be useful.  In general, however, I've always found it to be 
a major pain in the butt, to the point where I sometimes just punt and 
declare all my methods to "throw Exception" (or whatever the correct syntax 
is).  Not to mention that with a dynamic language like Python, it's 
probably impossible to implement.

I think the real problem here is that the on-line docs are incomplete 
because they don't list all the exceptions that this module can raise.  The 
solution to that is to open a bug on sourceforge against the docs.



More information about the Python-list mailing list