Avoiding a potential problem with an except: clause
There are some common mistakes in exception handling. These can cause programs to become unresponsive.
One of the mistakes we can make is to use the except:
clause. There are a few other mistakes which we can make if we're not cautious about the exceptions we try to handle.
This recipe will show some common exception handling errors that we can avoid.
Getting ready
In the Avoiding a potential problem with an except: clause recipe we looked at some considerations when designing exception handling. In that recipe, we discouraged the use of BaseException
because we can interfere with stopping a misbehaving Python program.
We'll extend the idea of what not to do in this recipe.
How to do it...
Use except Exception:
as the most general kind of exception managing.
Handling too many exceptions can interfere with our ability to stop a misbehaving Python program. When we hit Ctrl + C, or send a SIGINT
signal via kill -2
, we generally want the program to...