For example:
try: raise KeyError
except KeyError: pass
From the documentation:
raise evaluates the first expression as the exception object. It must be either a subclass or an instance of BaseException. If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments.
Some exception classes require arguments so the naked raise will fail with a TypeError.
raise UnicodeDecodeError
Traceback (most recent call last):
File "", line 1, in
TypeError: function takes exactly 5 arguments (0 given)
Traceback (most recent call last):
File "
TypeError: function takes exactly 5 arguments (0 given)
Seems to me it is clearer to just do the raise with instance syntax:
try:
raise KeyError()
except KeyError as e:
pass
Don't you mean "raise KeyError()" with parens?
ReplyDeleteYep, I did. Fixed now.
ReplyDelete