Chained Catch Statements

Era Scarecrow rtcvb32 at
Mon Jan 30 08:55:58 PST 2012

On Monday, 30 January 2012 at 14:50:23 UTC, Adam D. Ruppe wrote:
> On Monday, 30 January 2012 at 14:37:19 UTC, Jared wrote:
>> In Java and C++, I can do something to the effect of:
> That works in D too.
> I believe it does it linearly though, so it will use the
> first catch that matches.
> try {}
> catch (Exception e) {} // most throwable objects derive from 
> Exception
> catch (SpecialException e) {} // never used, because Exception 
> matches it all
> Try putting the more specific catches first, and the generic
> base classes at the end of the list.

To me this seems like a mistake. Since likely your catching the 
current exception and not one of the previously stored ones; A 
codepath like that should either:

A) Fail at compile time, hopefully telling you a suggested order 
so there's no problems.

B) Reorder the catch blocks on it's own during compile time, 
since only one can get caught at a time anyways.

At least that's how I see it.

More information about the Digitalmars-d-learn mailing list