Are there any unexpected side effects of using "except None"? The behavior I expect is that nothing will be caught by that clause which a few small tests seem to confirm.
Here is a rough outline of what I am trying to do. When no argument is provided to the function, exceptions=None which creates the "except None" clause. Just want to double check that I'm not going to catch something unexpected.
# exceptions is exception or set of exceptions I want to do special processing for
def check_exceptions(exceptions=None)
try:
...
except exceptions as e:
...
Use pass to ignore an exception Within a try and except block, use pass in the except clause to indicate no action is required in handling the caught exception.
Catching Exceptions in Python In Python, exceptions can be handled using a try statement. The critical operation which can raise an exception is placed inside the try clause. The code that handles the exceptions is written in the except clause.
There is no "invalid argument" or "null pointer" built-in exception in Python. Instead, most functions raise TypeError (invalid type such as NoneType ) or ValueError (correct type, but the value is outside of the accepted domain).
The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.
Works fine here (under Python 2.x).
>>> try:
... foo
... except None as e:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'foo' is not defined
For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, or a tuple containing an item compatible with the exception.
source
Therefore the expression doesn't have to be an exception type, it will simply fail to ever match.
This behavior was changed in Python 3.x, and the expression after except
must be a descendant of BaseException
or a tuple of such.
def check_exceptions(exceptions=())
try:
...
except exceptions as e:
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With