I would like to know the best practice about raising an exception without arguments. In the official python documentation, you can see this :
try:
raise KeyboardInterrupt
(http://docs.python.org/tutorial/errors.html chap. 8.6)
and in some differents code, like Django or Google code, you can see this :
def AuthenticateAndRun(self, username, password, args):
raise NotImplementedError()
(http://code.google.com/p/neatx/source/browse/trunk/neatx/lib/auth.py)
The exception is instanciate before being raised while there is no argument. What is the purpose to instanciate an exception without arguments ? When I should use the first case or the second case ?
Thanks in advance Fabien
Only an exception handler (or a function that a handler calls, directly or indirectly) can use raise without any expressions.
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. We can thus choose what operations to perform once we have caught the exception.
raise allows you to throw an exception at any time. assert enables you to verify if a certain condition is met and throw an exception if it isn't. In the try clause, all statements are executed until an exception is encountered. except is used to catch and handle the exception(s) that are encountered in the try clause.
The raise keyword is used to raise an exception. You can define what kind of error to raise, and the text to print to the user.
In Python 3 there are 4 different syntaxes of raising exceptions. raise exception – No argument print system default message. raise exception (args)– with an argument to be printed. raise – without any arguments re-raises the last exception. raise exception (args) from original_exception – contain the details of the original exception.
Syntax 1 raise exception – No argument print system default message 2 raise exception (args) – with an argument to be printed 3 raise – without any arguments re-raises the last exception 4 raise exception (args) from original_exception – contain the details of the original exception
Why use Argument in Exceptions? It can be used to gain additional information about the error encountered. As contents of an Argument can vary depending upon different types of Exceptions in Python, Variables can be supplied to the Exceptions to capture the essence of the encountered errors.
Exceptions are objects in Python, so you can assign the exception that was raised to a variable. This way, you can print the default description of the exception and access its arguments. According to the Python Documentation: The except clause may specify a variable after the exception name.
You can use whichever form you like. There is no real difference and both are legal in Python 2 and 3. Python style guide does not specify which one is recommended.
A little more information on the "class form" support:
try:
raise KeyboardInterrupt
This form is perfectly legal in both Python 2 and 3. Excerpt from pep-3109:
raise EXCEPTION is used to raise a new exception. This form has two sub-variants: EXCEPTION may be an exception class or an instance of an exception class; valid exception classes are BaseException and its subclasses [5]. If EXCEPTION is a subclass, it will be called with no arguments to obtain an exception instance.
It is also described in Python documentation:
... If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments.
Raising an exception class instead of an exception instance is deprecated syntax and should not be used in new code.
raise Exception, "This is not how to raise an exception..."
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