[Python-Dev] Style for raising exceptions (python-dev Summary for 2005-08-01 through 2005-08-15 [draft])

Guido van Rossum gvanrossum at gmail.com
Thu Aug 25 17:17:24 CEST 2005

On 8/25/05, M.-A. Lemburg <mal at egenix.com> wrote:
> I must have missed this one:
> > ----------------------------
> > Style for raising exceptions
> > ----------------------------
> >
> > Guido explained that these days exceptions should always be raised as::
> >
> >     raise SomeException("some argument")
> >
> > instead of::
> >
> >     raise SomeException, "some argument"
> >
> > The second will go away in Python 3.0, and is only present now for backwards
> > compatibility.  (It was  necessary when strings could be exceptions, in
> > order to pass both the exception "type" and message.)   PEPs 8_ and 3000_
> > were accordingly updated.
> AFAIR, the second form was also meant to be able to defer
> the instantiation of the exception class until really
> needed in order to reduce the overhead related to raising
> exceptions in Python.
> However, that optimization never made it into the implementation,
> I guess.

Something equivalent is used internally in the C code, but that
doesn't mean we'll need it in Python code. The optimization only works
if the exception is also *caught* in C code, BTW (it is instantiated
as soon as it is handled by a Python except clause).

Originally, the second syntax was the only available syntax, because
all we had were string exceptions. Now that string exceptions are dead
(although not yet buried :) I really don't see why we need to keep
both versions of the syntax; Python 3.0 will only have one version.
(We're still debating what to do with the traceback argument; wanna
revive PEP 344?)

If you need to raise exceptions fast, pre-instantiate an instance.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list