Chained Catch Statements

Era Scarecrow rtcvb32 at
Mon Jan 30 10:23:55 PST 2012

On Monday, 30 January 2012 at 17:17:46 UTC, Andrej Mitrovic wrote:
> On 1/30/12, Era Scarecrow <rtcvb32 at> wrote:
>> To me this seems like a mistake.
> You could throw SpecialException in the handler for Exception. 
> So it's
> legal code.

Yes, thanks to inheritance it is legal code. However it's almost 
the same as this, which is legal code too.

try {
} catch (Throwable t) {

} catch {Exception e) { //never executed

} catch (StreamException st) { //never executed

} //and the list can go on forever.

See the problem? Everything that COULD be caught by exception is 
now caught by throwable since it's inherited. At the very least 
the compiler should warn you that certain sections won't be 
executed, But then I would have to manually look up the 
relationships when the compiler already knows them. In GCC known 
code blocks that are never executed aren't even compiled in. If 
the compiler reorders the blocks for you, the above would become 

try {
} catch (StreamException st) { //specific case

} catch {Exception e) { //less specific but no other inheritances 
of this kind

} catch (Throwable t) { //everything else.


More information about the Digitalmars-d-learn mailing list