On Fri, Feb 7, 2020 at 7:56 AM Ethan Furman <et...@stoneleaf.us> wrote:
> On 02/07/2020 07:33 AM, Serhiy Storchaka wrote: > > 07.02.20 16:28, Ram Rachum пише: > >> The idea is to add `raise as` syntax, that raises an exception while > setting the currently caught exception to be the cause. It'll look like > this: > >> > >> try: > >> 1/0 > >> except ZeroDivisionError: > >> raise as ValueError('Whatever') > >> > >> What it does is a shorter version of this: > >> > >> try: > >> 1/0 > >> except ZeroDivisionError as error: > >> raise ValueError('Whatever') from error > > > > How does it differ from > > > > try: > > 1/0 > > except ZeroDivisionError: > > raise ValueError('Whatever') > > Here's a diff: > > $ diff no_from.py with_from.py > 3,4c3,4 > - ... except ZeroDivisionError: > - ... raise ValueError('Whatever') > --- > + ... except ZeroDivisionError as e: > + ... raise ValueError('Whatever') from e > 10c10 > - During handling of the above exception, another exception occurred: > --- > + The above exception was the direct cause of the following exception: > > > To me the difference between "raise from" and "raise" is the implicit > meaning: > > - "During handling of ..." implies that the second exception should not > have occurred and there is a bug in the exception handling code > > - "The above exception ..." implies that this code path is normal and > extra information is being supplied > They also set different attributes on the exception to communicate the trigger of the exception, so there's also a structural difference beyond just the textual one.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/7QDKAXAWIOKCMX5DZUKDVYJ5QJ4GSPB2/ Code of Conduct: http://python.org/psf/codeofconduct/