[Python-Dev] PEP for allowing 'raise NewException from None'

Nick Coghlan ncoghlan at gmail.com
Sat Jan 28 07:31:41 CET 2012


On Sat, Jan 28, 2012 at 10:33 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> Because at this point it is possible to do:
>
>    raise ValueError from NameError
>
> outside a try block.  I don't see it as incredibly useful, but I don't know
> that it's worth making it illegal.
>
> So the question is:
>
>  - should 'raise ... from ...' be legal outside a try block?
>
>  - should 'raise ... from None' be legal outside a try block?

Given that it would be quite a bit of work to make it illegal, my
preference is to leave it alone.

I believe that means there's only one open question. Should "raise ex
from None" be syntactic sugar for:

1. clearing the current thread's exception state (as I believe Ethan's
patch currently does), thus meaning that __context__ and __cause__
both end up being None
2. setting __cause__ to None (so that __context__ still gets set
normally, as it is now when __cause__ is set to a specific exception),
and having __cause__ default to a *new* sentinel object that indicates
"use __context__"

I've already stated my own preference in favour of 2 - that approach
means developers that think about it can explicitly change exception
types such that the context isn't displayed by default, but
application and framework developers remain free to insert their own
exception handlers that *always* report the full exception stack.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list