150

Defining a parameterless exception:

class MyException(Exception): pass 

When raised, is there any difference between:

raise MyException 

and

raise MyException() 

I couldn't find any; is it simply an overloaded syntax?

2
  • 1
    Read this answer: stackoverflow.com/questions/13052991/… Commented May 23, 2013 at 6:49
  • 2
    Strictly speaking it's not syntactic. Python cannot know whether it will get a class or an instance until runtime. Commented Jul 31, 2014 at 1:01

3 Answers 3

161

The short answer is that both raise MyException and raise MyException() do the same thing. This first form auto instantiates your exception.

The relevant section from the docs says:

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.

That said, even though the semantics are the same, the first form is microscopically faster, and the second form is more flexible (because you can pass it arguments if needed).

The usual style that most people use in Python (i.e. in the standard library, in popular applications, and in many books) is to use raise MyException when there are no arguments. People only instantiate the exception directly when there some arguments need to be passed. For example: raise KeyError(badkey).

Sign up to request clarification or add additional context in comments.

2 Comments

Why would the first form (without parentheses) be microscopically faster?
@jamesdlin Because the C code to auto-instantiate don't have the interpreted overhead than comes from you making the call yourself.
5

Go look at the docs for the raise statement. It's creating an instance of MyException.

1 Comment

It's worth noting that the syntax for raise has changed a bit in Python 3. The part that's relevant to this question is the same though (raise ExceptionType still creates an instance of the type by calling its constructor with no arguments).
-3

Yep, there is a difference between ValueError and ValueError()

ValueError is a class whereas ValueError() creates an instance of a class. This is the reason the type(ValueError) is type and type(ValueError()) is ValueError

The sole purpose of raise is to raise the exception,

when we use ValueError, class will be called which in turn runs the constructor ValueError()

when we use ValueError(), the method ValueError() is directly called.

Note: raise ValueError # shorthand for 'raise ValueError()'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.