Search code examples
pythonexceptionargumentsraise

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


Solution

  • 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..."