On Fri, Feb 7, 2020 at 7:56 AM Ethan Furman <ethan@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
- ... except ZeroDivisionError:
- ...     raise ValueError('Whatever')
+ ... except ZeroDivisionError as e:
+ ...     raise ValueError('Whatever') from e
- 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.