When is bare except: justified?
John J. Lee
jjl at pobox.com
Fri Dec 5 15:38:01 EST 2003
"Dave Brueck" <dave at pythonapocrypha.com> writes:
> > When *is* it justified to use except:, then?
[...]
> The rule of thumb I use is that a bare except is bad if it hides the fact that
> an exception occurred. So, for example, using a bare except in code like this
> is okay:
>
> for worker in workers:
> try:
> worker.process()
> except:
> LogException()
>
> It's considered "okay" because even though the code prevents the exception from
> propagating, it doesn't lose record of the fact that something went wrong.
[...]
Now that you say that, it's obvious :-) In fact, IIRC just the other
day I fixed code to work that way in the same package I discussed in
my post:
try:
__import__(module_name)
except ImportError:
+ traceback.print_exc()
sys.exit("Import of test module failed -- Couldn't find tests?")
(this is in a hack to import all tests from a bunch of test_foo.py files)
I guess I can keep the bare except:s I discussed in the first part of
my post after all, but just make sure I emit a warning (with the
warnings module in 2.3, I suppose).
Thanks!
John
More information about the Python-list
mailing list