How to effectively use exception

Steven Haryanto steven at haryan.to
Sat Apr 14 07:18:37 EDT 2001


So far I have understood one rule of thumb about exceptions:
"Catch as few of them as possible in the inner blocks of the
program. The uncaught exceptions will creep up to the upper
blocks until, at one level, it will be caught by the code that
know how to handle it. The main block should catch all the
rest and act accordingly.

There are two things that I seek advice on:

First, should we use exceptions whenever possible? For example,
should a smallish function that do some testing return 1/0 to
indicate success/failure or should it raise ValueError if the
test fails? Is the try-except-else style more Pythonic/
recommended than the if-else-then style? If not, what are the
considerations to balance the two?

Second, when do we create our own specific Exception classes,
and when do we just use the existing standard Exception (I use
ValueError in a lot of my methods & functions, accompanied by
the specific message string. So far I haven't faced with the
need to create a custom Exception class).

Lastly, can someone recommend some readings on this matter?

Thanks, and a happy easter holiday to everybody,
Steve





More information about the Python-list mailing list