Friday, December 5, 2003


>>> class MyExc(Exception):
...   def __init__(self, v):
...     print "Exception '", v, "'."
... 
>>> raise MyExc("Foobar")
Exception ' Foobar '.
Traceback (most recent call last):
  File "", line 1, in ?
__main__.MyExc
>>> raise MyExc, "Foobar"
Exception ' Foobar '.
Traceback (most recent call last):
  File "", line 1, in ?
__main__.MyExc

Raise allows for both the raise FooExc, "data" and raise FooExc("data") style of raising an exception.

This seems odd. Python generally focuses on one way to do any one thing. Having two ways of expressing raising of an exception where both appear to do the same thing and neither is obviously syntactical superior seems kind of non-pythonic.

Do they do something different? Is there some reason why both syntactic forms exist? ... backwards compatibility?

Enquiring minds want to know...

Update: The mysterious g pontificated a reference to PEP 317: Eliminate Implicit Exception Instantiation. PEP 317 was rejected and the long winded discussion that led to the rejection starts here and created a boatload of list traffic.

In any case, 317 was rejected. As such, the original syntax has been preserved and the new syntax remains.

Makes sense. Deprecating-- and eventually eliminating-- the syntax would have required revisiting the bazillions of lines of python code that already exist.
3:36:47 PM  pontificate