"raise (type, value, traceback)" and "raise type, value, traceback"
Duncan Booth
duncan.booth at invalid.invalid
Tue May 3 04:41:04 EDT 2011
Ian Kelly <ian.g.kelly at gmail.com> wrote:
> t = (type, value, traceback)
> raise t
>
> That it accepts the tuple and raises a value-less expression of type
> `type` surprises me. The docs don't say anything about this, and I
> would have expected a TypeError, but it appears to be extracting the
> first element of the tuple and using that as the value of the first
> expression.
>
Not only that but you can nest tuples and it will drill down as far as
necessary through the tuples until it finds something that isn't a
tuple.
It doesn't appear to be documented, but I think it may be intended to
provide some kind of symmetry with 'try...except': when you catch an
exception the expression used to catch the exception must be
'compatible' with the exception, i.e. the exception itself, one of its
base classes, or a tuple which contains an item 'compatible' with the
exception.
So my guess would be that since it allows:
MyExceptionSpec = (RuntimeError, ValueError)
try:
...
except MyExceptionSpec:
...
the intent is to allow you to also throw 'MyExceptionSpec' from within
the 'try'.
I would also guess that it is a hangover from the days when exceptions
were strings and you couldn't use inheritance to group exceptions.
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list