Exception chaining

Sean Kelly sean at invisibleduck.org
Fri Sep 13 16:31:43 PDT 2013


On Sep 13, 2013, at 2:14 PM, monarch_dodra <monarchdodra at gmail.com> wrote:

> In one of my exception handling blocks, I call some code that could *also*, potentially throw (it's actually a loop, where each iteration can throw, but I have to do them *all*, meaning I need to handle *several* extra exceptions). I'm wondering what the "correct" way of handling both exceptions is.
> 
> We can chain them, but which comes first? Ideally, it'd be first in, first out, but given that exceptions make a singly linked list, pushing back is expensive (well, as expensive as it can get in exception handling code I guess).

Exception chaining is actually built-in.  I did some digging for an official description, but couldn't find one.  Here's a summary:

If an Exception is thrown when an Exception or Error is already in flight, it is considered collateral damage and appended to the chain (built via Throwable.next).

If an Error is thrown when an Exception is already in flight, it will replace the in flight Exception and reference it via Error.bypassedException.

If an Error is thrown when an Error is already in flight, it will be considered collateral damage and appended to the Throwable.next chain.


So in the case where an Exception is thrown, more Exceptions are generated as collateral damage, then an Error is thrown which bypasses the in flight exception, and then more Exceptions and Errors are generated as collateral damage, you can have one chain off the primary Error and a second chain of the bypassedException.  It's possible that bit needs to be cleaned up so there's only one chain.


More information about the Digitalmars-d-learn mailing list