[Python-ideas] Improving Catching Exceptions
Sven R. Kunze
srkunze at mail.de
Thu Jun 22 16:30:57 EDT 2017
Hi folks,
just one note I'd like to dump here.
We usually teach our newbies to catch exceptions as narrowly as
possible, i.e. MyModel.DoesNotExist instead of a plain Exception. This
works out quite well for now but the number of examples continue to grow
where it's not enough.
There are at least three examples I can name off the top of my head:
1) nested StopIteration - PEP 479
2) nested ImportError
3) nested AttributeError
1) is clear. 2) usually can be dealt with by applying the following pattern:
try:
import user
except ImportError:
import sys
if sys.exc_info()[2].tb_next:
raise
Chris showed how to deal with 3). Catching nested exception is not what
people want many times.
Am I the only one getting the impression that there's a common theme here?
Regards,
Sven
More information about the Python-ideas
mailing list