I often see comments on other Stack Overflow questions about how the use of except: pass
is discouraged. Why is this bad? Sometimes I just don't care what the errors, are and I want to just continue with the code.
try:
something
except:
pass
Why is using an except: pass
block bad? What makes it bad? Is it the fact that I pass
on an error or that I except
any error?
This catches every possible exception, including
GeneratorExit
,KeyboardInterrupt
, andSystemExit
- which are exceptions you probably don't intend to catch. It's the same as catchingBaseException
.Older versions of the documentation say:
Python Exception Hierarchy
If you catch a parent exception class, you also catch all of their child classes. It is much more elegant to only catch the exceptions you are prepared to handle.
Here's the Python 3 exception hierarchy - do you really want to catch 'em all?:
Don't Do this
If you're using this form of exception handling:
Then you won't be able to interrupt your
something
block with Ctrl-C. Your program will overlook every possible Exception inside thetry
code block.Here's another example that will have the same undesirable behavior:
Instead, try to only catch the specific exception you know you're looking for. For example, if you know you might get a value-error on a conversion:
Further Explanation with another example
You might be doing it because you've been web-scraping and been getting say, a
UnicodeError
, but because you've used the broadest Exception catching, your code, which may have other fundamental flaws, will attempt to run to completion, wasting bandwidth, processing time, wear and tear on your equipment, running out of memory, collecting garbage data, etc.If other people are asking you to complete so that they can rely on your code, I understand feeling compelled to just handle everything. But if you're willing to fail noisily as you develop, you will have the opportunity to correct problems that might only pop up intermittently, but that would be long term costly bugs.
With more precise error handling, you code can be more robust.
So, here is my opinion. Whenever you find an error, you should do something to handle it, i.e. write it in logfile or something else. At least, it informs you that there used to be a error.
You should use at least
except Exception:
to avoid catching system exceptions likeSystemExit
orKeyboardInterrupt
. Here's link to docs.In general you should define explicitly exceptions you want to catch, to avoid catching unwanted exceptions. You should know what exceptions you ignore.
All comments brought up so far are valid. Where possible you need to specify what exactly exception you want to ignore. Where possible you need to analyze what caused exception, and only ignore what you meant to ignore, and not the rest. If exception causes application to "crash spectacularly", then be it, because it's much more important to know the unexpected happened when it happened, than concealing that the problem ever occurred.
With all that said, do not take any programming practice as a paramount. This is stupid. There always is the time and place to do ignore-all-exceptions block.
Another example of idiotic paramount is usage of
goto
operator. When I was in school, our professor taught usgoto
operator just to mention that thou shalt not use it, EVER. Don't believe people telling you that xyz should never be used and there cannot be a scenario when it is useful. There always is.Since it hasn't been mentioned yet, it's better style to use
contextlib.suppress
:Notice that in the example provided, the program state remains the same, whether or not the exception occurs. That is to say,
somefile.tmp
always becomes non-existent.Handling errors is very important in programming. You do need to show the user what went wrong. In very few cases you can ignore the errors. This is it is very bad programming practice.