Chaining exceptions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Nov 18 15:24:11 PST 2009


Consider:

void fun() {
    try {
       throw new Exception("a");
    } finally {
       throw new Exception("b");
    }
}

Currently this function would unceremoniously terminate the program. I 
think it shouldn't. What should happen is that the "a" exception should 
be propagated unabated, and the "b" exception should be appended to it. 
The Exception class should have a property "next" that returns a 
reference to the next exception thrown (in this case "b"), effectively 
establishing an arbitrarily long singly-linked list of exceptions.

A friend told me that that's what Java does, with the difference that 
the last exception thrown takes over, so the chain comes reversed. I 
strongly believe "a" is the main exception and "b" is a contingent 
exception, so we shouldn't do what Java does. But Java must have some 
good reason to go the other way.

Please chime in with (a) a confirmation/infirmation of Java's mechanism 
above; (b) links to motivations for Java's approach, (c) any comments 
about all of the above.


Thanks,

Andrei



More information about the Digitalmars-d mailing list