exceptions beyond errors?

Terry Reedy tjreedy at udel.edu
Mon Jun 9 09:35:33 EDT 2003


"e y suzuki" <suzuki-python at pixar.com> wrote in message
news:mailman.1055136735.26198.python-list at python.org...
>
> i'm curious to know about ways that one could use exceptions
> in python programs aside from just detecting errors.

In some programming languages, there is apparently a feeling that
'exception' == 'error', but this is not so in Python, any more than
exceptional talent is errorneous.  Exceptions are useful anytime
something needs special treatment.

> are there additional non-error-detection cases where exceptions
> are useful?

A once common idiom was

try: x = mydict[key]
except: x = defaultvalue

This has now been incorporated in the dict.get method so one can
instead write

x = mydict.get(key, defaultvalue), with defaultvalue defaulting to
None.

Terry J. Reedy







More information about the Python-list mailing list