Which exception to use?

Peter Hansen peter at engcorp.com
Wed Jan 29 10:19:56 EST 2003


Michael Hudson wrote:
> 
> Chad Netzer <cnetzer at mail.arc.nasa.gov> writes:
> 
> > I do things like this for some library functions that I create (I
> > welcome discussion about whether it is a good approach)
> >
> > class ArgumentError( Exception ):
> >     pass
> 
> This *is* a good approach (IMHO), but you should consider deriving
> your exceptions from StandardError instead (I rarely remember to do
> this, either).

Why should you do this?

I found the following quote when I searched for "python standarderror",
from http://web.pydoc.org/1.5.2/exceptions.html#StandardError :

  "If you define your own class based exceptions, they should be 
   derived from Exception."

All of *Python's* exceptions, except SystemExit, are derived from
StandardError, but the description of StandardError says only 
"Base class for all standard Python exceptions.".

Since we're not talking about defining *standard* exceptions, 
shouldn't we follow the advice in that page?

-Peter




More information about the Python-list mailing list