Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to raise exception in python? [duplicate]

Tags:

Here is the simple code:

import sys

class EmptyArgs(StandardError):
    pass

if __name__ == "__main__":
    #first way to raise exception
    if len(sys.argv) == 1:
       raise EmptyArgs     
    #second way to raise exception
    if len(sys.argv) == 1:
       raise EmptyArgs()

Which way is "more" correct? Both are working.
Note: In my real code, exception is exactly the same as I declared: without message and arguments.

like image 735
Anton Koval' Avatar asked Oct 24 '12 15:10

Anton Koval'


People also ask

How do you raise exceptions in Python?

As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.

How do you raise multiple exceptions in Python?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Can you raise multiple exceptions?

Strictly speaking you can't raise multiple exceptions but you could raise an object that contains multiple exceptions. Question: Why would you ever want to do this? Answer: In a loop when you want to raise an error but process the loop to completion.

How do I manually raise exceptions?

Throwing exceptions manually There are two types of exceptions user defined and predefined each exception is represented by a class and which inherits the Throwable class. To throw an exception explicitly you need to instantiate the class of it and throw its object using the throw keyword.


2 Answers

Both are proper; the latter form let's you attach arguments to your exception:

if len(sys.argv) == 1:
   raise EmptyArgs('Specify at least 1 argument')

You can also pass in the arguments as a second value as a tuple in the raise statement:

if len(sys.argv) == 1:
   raise EmptyArgs, ('Specify at least 1 argument',)

but a single non-tuple value will work too, and is regarded as a single argument:

if len(sys.argv) == 1:
   raise EmptyArgs, 'Specify at least 1 argument'

and a third value to raise let's you specify an alternate traceback, which then is used instead of a traceback that would be generated for the current location in the code:

if len(sys.argv) == 1:
   raise EmptyArgs, ('Specify at least 1 argument',), traceback_object

See the documentation for the raise statement

Note that when you do use arguments for your exception, The Python styleguide PEP 8 prefers you provide an exception instance, and not a class:

When raising an exception, use raise ValueError('message') instead of the older form raise ValueError, 'message'.

The paren-using form is preferred because when the exception arguments are long or include string formatting, you don't need to use line continuation characters thanks to the containing parentheses. The older form will be removed in Python 3.

Python 3 will no longer support that form.

like image 160
Martijn Pieters Avatar answered Sep 23 '22 08:09

Martijn Pieters


It is NOT recommended to raise Exception without arguments i.e. raising the exception class is not the way to go. Just do something like this

raise MyException()

because in Python 3.0 similar case has been removed for good

raise Exception, "foo"

There should be one-- and preferably only one --obvious way to do it.

like image 32
Anurag Uniyal Avatar answered Sep 23 '22 08:09

Anurag Uniyal