Raise X or Raise X()?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Mar 11 19:59:09 EDT 2012


On Sun, 11 Mar 2012 12:04:55 -0700, bvdp wrote:

> Which is preferred in a raise: X or X()? 

Both.

Always use raise "X(*args)" when you need to provide arguments (which you 
should always do for exceptions meant for the caller to see). The form 
"raise X, args" should be considered discouraged, and in fact is gone in 
Python 3.x.

Purely internal exceptions (which you raise and catch yourself) don't 
need arguments, so there is no difference between the two forms: 
"raise X" is exactly equivalent to "raise X()" with no arguments. Use 
whichever takes your fancy.

Personally, I used "raise X" to mean "this doesn't need arguments and 
should never have any" and "raise X()" to mean "this needs arguments but 
I'm too lazy to provide them right now". Think of it as a FIXME.


-- 
Steven



More information about the Python-list mailing list