How do I know all thrown exceptions of a function?

Delaney, Timothy tdelaney at avaya.com
Mon Jan 22 20:12:10 EST 2001


> I don't understand "things which should normally pass through (such
> as index-out-of-bounds)".  Pass through what?  When you later say
> "so they don't need to be declared in just about every 
> function around",
> does that account for the whole reason behind SilentException?

Well, the "index-out-of-bounds" (currently IndexError) is something that you
don't want to have every function declaring - but almost every function has
the potential for throwing it. Every time you do an indexed access there is
the chance of throwing the exception. Since indexing is such a common thing
to do and normally you don't need to worry about falling off the end, you
make it "silent". In Java terminology, this is a "RuntimeException".

> I don't understand the distinction between Exception and Error.
> Would you be able to catch Error the same as Exception?  If not,
> what happens?  If you can catch them the same as Exception, then
> it's just another category invented so you won't have to declare
> the exception?

No. You only put things as Errors if they are "the world is dying" type
things. This means you can do

try:
except Exception:

and Errors don't get trapped - only Exceptions and SilentExceptions. Things
which qualify for "Error" status include "the computer has run out of
memory, both physical and virtual".

It's still possible to catch such errors, but you'd better be sure you know
how to deal with them. Another good example of an error is "the module you
tried to import cannot be found". This is normally not an expected exception
- it's normally a "program must die" error. However, sometimes you want to
catch the error and import a different module.

Tim Delaney
Avaya Australia




More information about the Python-list mailing list