[Python-Dev] exception chaining

Georg Brandl g.brandl at gmx.net
Fri Jan 20 22:07:54 CET 2012


Am 20.01.2012 21:05, schrieb Ethan Furman:
> Benjamin Peterson wrote:
>> 2012/1/20 Ethan Furman <ethan at stoneleaf.us>:
>>> Summary:
>>>
>>> Exception Chaining is cool, unless you are writing libraries that want to
>>> transform from Exception X to Exception Y as the the previous exception
>>> context is unnecessary, potentially confusing, and cluttery (yup, just made
>>> that word up!).
>>>
>>> For all the gory details, see http://bugs.python.org/issue6210.
>>>
>>> I'm going to attempt a patch implementing MRAB's suggestion:
>>>
>>> try:
>>>    some_op
>>> except ValueError:
>>>    raise as OtherError() # `raise` keeps context, `raise as` does not
>> 
>> I dislike this syntax. Raise what as OtherError()? I think the "raise
>> x from None" idea is preferable, since it indicates you are nulling
>> the context. The optimal solution would be to have "raise X
>> nocontext", but that would obviously require another keyword...
> 
> Raise 'the error' as OtherError.
> 
> The problem I have with 'raise x from None' is it puts 'from None' clear 
> at the end of line -- not a big deal on this very short example, but 
> when you have actual text it's not as obvious:

Well, the "as" in "raise as" would be very easily overlooked too.

> except SomeError():
>      raise SomeOtherError('explanatory text with actual %data to help 
> track down the problem' % data) from None

In any case, I don't think the context suppression is the most important
thing about the exception raising, so it doesn't need to stand out...

Georg



More information about the Python-Dev mailing list