try..except with empty exceptions
steve+comp.lang.python at pearwood.info
Sun Apr 12 06:18:47 CEST 2015
On Sun, 12 Apr 2015 09:08 am, Cameron Simpson wrote:
> Also, IMO, a bare "except:" syntax is far more pleasing to the eye than
> "except magic_exception_name_that+gets_everything:".
And that is exactly what makes bare excepts an attractive nuisance!
I'm going to channel a newbie, cowboy or just plain lazy coder:
"I have a block of code with a bug. Bugs are bad. I know, I'll wrap it in
and the bug will go away!"
It looks nice and requires relatively little typing. It seems like something
you should do, but it doesn't fix the problem, chances are it just makes it
worse. I've spotted many real-world examples where bare excepts mask the
presence of actual bugs, where the intention is clearly to catch a single
# empty list
Forcing people to type an exception will discourage such cowboy coding. If
the choice is between
instead of the lazy "except:" version, and
which do you think people will write?
> Also, I wish
> "BaseException" were just spelled "Exception", if it has to be used.
Most of the time, "catch everything" should mean catching Exception, not
BaseException. Broadly speaking, built-in exceptions which are
considered "errors" inherit from Exception, and the small number that don't
are used for some variation of control-flow:
Most of the time when people say "catch everything" they mean catch all
errors, i.e. Exception, rather than "don't let the user interrupt the code
See also PEP 352:
More information about the Python-list