Find a compiler construction textbook and look-up the dangling-else ambiguity.
Given that in Java, and most other languages with horrible syntax, spacing lies. How do you interpret:
try
try
stuff();
catch (FooException exc)
handle(exc);
catch (BarException exc)
handle(exc);
catch (BazException exc)
handle(exc);
Is it:
try {
try {
stuff();
} catch (FooException exc) {
handle(exc);
} catch (BarException exc) {
handle(exc);
}
} catch (BazException exc) {
handle(exc);
}
Or:
try {
try {
stuff();
} catch (FooException exc) {
handle(exc);
}
} catch (BarException exc) {
handle(exc);
} catch (BazException exc) {
handle(exc);
}
The dangling-else ambiguity is resolved by associating the else
with the inner-most if
. Do we want to add a more complicated complication to handle this poor style? No.
Edit: There's a comment that the example does not cover catch
. It would be a proper weird decision to require braces on try
but not catch
/finally
. But anyway, for completeness, consider the following code.
try {
stuff();
} catch (FooException foo)
try {
handle(foo);
} catch (BarException bar)
handle(bar);
catch (BazException baz)
handle(baz);
finally
release();
Is the catch
of BazException
and finally
associated with the inner or outer try
? Again the language design committee could have added a ton of grammar to disambiguate, but again explicit style wins. My job at Sun/Oracle would have been a little easier if the language had been simplified to mandate explicit braces everywhere.
e = thrownException;
means? We are declaringe
as an Exception in the parameter block then why does this statement is needed? – Bellworte
with the exception that has occurred and then do the rest processing. Am I right? – Bellwortif(condition);
(notice the semicolon at the end) – Aliuswe
part, poor exception handling is just poor exception handling. – Stampede