Exception Handling in Python 3
Ben Finney
ben+python at benfinney.id.au
Sun Oct 24 19:51:59 EDT 2010
Steve Holden <steve at holdenweb.com> writes:
> I simply felt that the traceback gives too much information in the
> case where an exception is specifically being raised to replace the
> one currently being handled.
Ideally, that description of the problem would suggest the obvious
solution: replace the class of the exception and allow the object to
continue up the exception handler stack.
But that doesn't work either::
>>> d = {}
>>> try:
... val = d['nosuch']
... except KeyError as exc:
... exc.__class__ = AttributeError
... raise exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyError: 'nosuch'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
TypeError: __class__ assignment: only for heap types
which means, AFAICT, that re-binding ‘__class__’ is only allowed for
objects of a type defined in the Python run-time heap, not those defined
in C code (like the built-in-exception types).
--
\ “I wish there was a knob on the TV to turn up the intelligence. |
`\ There's a knob called ‘brightness’ but it doesn't work.” |
_o__) —Eugene P. Gallagher |
Ben Finney
More information about the Python-list
mailing list