Which exception to use?

Michael Hudson mwh at python.net
Thu Jan 30 06:59:55 EST 2003


Peter Hansen <peter at engcorp.com> writes:

> 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 think I'm mixed up.

> 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?

My idea was that instead of writing

except:

you should mostly write

except StandardError:

so you don't catch SystemExits by mistake.  But it looks like I've
been making things up again, so feel free to ignore my post...

Cheers,
M.

-- 
  Arrrrgh, the braindamage!  It's not unlike the massively
  non-brilliant decision to use the period in abbreviations
  as well as a sentence terminator.  Had these people no
  imagination at _all_?                 -- Erik Naggum, comp.lang.lisp




More information about the Python-list mailing list