How do I know all thrown exceptions of a function?

Remco Gerlich scarblac at pino.selwerd.nl
Tue Jan 23 13:48:45 EST 2001


Sean Reifschneider <jafo at tummy.com> wrote in comp.lang.python:
> On Sun, Jan 21, 2001 at 01:02:36AM +0100, Syver Enstad wrote:
> >Couldn't you use the source?
> 
> Yeah, I suppose so.  I don't think you should *HAVE* to review the code of
> the libraries you call, AND *EVERYTHING* that *THEY* call.  It's not an
> effective use of my time...  I usually just end up leaving it and track
> down what exceptions get thrown later.  Luckily, Python exceptions are
> (almost) user-readable.

I've been thinking about good guidelines to solve this.

- Obviously, document all your error conditions.
- All the exceptions your code explicitly throws should inherit
  some base exception defined in your module, so that that can
  be tested for.
- Avoid throwing exceptions that have to do with your implementation
  to the outside world. If your module function gets an argument that
  is out of range, resulting in an IndexError, don't just pass that on
  to the caller but catch it and raise a custom exception yourself,
  that inherits from your module's standard exception. Hide your
  implementation.
- Obviously trying to change exceptions that aren't caused by your code
  (like KeyboardInterrupt) is of no use.

Comments?

-- 
Remco Gerlich



More information about the Python-list mailing list