Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, rasing an exception without arguments

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

like image 942
Fabien Engels Avatar asked Jul 12 '10 12:07

Fabien Engels


People also ask

Can we use raise without exception in Python?

Only an exception handler (or a function that a handler calls, directly or indirectly) can use raise without any expressions.

How do you handle a raise exception 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. We can thus choose what operations to perform once we have caught the exception.

What is a raising exception in Python?

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.

What is raise statement in Python?

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.

How do you raise an exception in Python?

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.

How do you raise an exception without an argument?

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 in Python?

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.

How do you assign an exception to a variable in Python?

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.


2 Answers

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.

like image 165
and Avatar answered Oct 11 '22 07:10

and


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..."
like image 39
Ignacio Vazquez-Abrams Avatar answered Oct 11 '22 08:10

Ignacio Vazquez-Abrams