[Python-ideas] Let try-except check the exception instance

Alexander Belopolsky alexander.belopolsky at gmail.com
Thu May 31 14:00:24 EDT 2018


On Thu, May 31, 2018 at 10:37 AM Nick Coghlan <ncoghlan at gmail.com> wrote:
>
> The exception machinery deliberately attempts to avoid instantiating
exception objects whenever it can, but that gets significantly more
difficult if we always need to create the instance before we can decide
whether or not the raised exception matches the given exception handler
criteria.


Is this really true?  Consider the following simple code

class E(Exception):
    def __init__(self):
        print("instantiated")

try:
    raise E
except E:
    pass

Is it truly necessary to instantiate E() in this case?  Yet when I run it,
I see "instantiated" printed on the console.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180531/dfa1c2d4/attachment.html>


More information about the Python-ideas mailing list