chain of exceptions, next method

Paul Backus snarwin at gmail.com
Sun Aug 14 02:30:43 UTC 2022


On Sunday, 14 August 2022 at 02:07:05 UTC, Ali Çehreli wrote:
>
> This automatic "combining" of exceptions happens for cleanup 
> code like scope(exit). (I remember bug(s) for scope(failure).):

To be precise, an exception thrown inside a 'finally' block gets 
chained onto the previous exception, but an exception thrown 
inside a 'catch' block does not. scope(exit) and scope(failure) 
are just syntax sugar for 'finally' and 'catch', respectively.

Relevant spec paragraph:

> If an exception is raised in the FinallyStatement and is not 
> caught before
> the original exception is caught, it is chained to the previous 
> exception via
> the next member of Throwable. Note that, in contrast to most 
> other
> programming languages, the new exception does not replace the 
> original
> exception. Instead, later exceptions are regarded as 
> 'collateral damage'
> caused by the first exception. The original exception must be 
> caught, and
> this results in the capture of the entire chain.

Source: https://dlang.org/spec/statement.html#try-statement

So, once an exception is caught, the chain ends, and any 
exception thrown after that begins a new chain.


More information about the Digitalmars-d-learn mailing list