javac warnings for multi-catch
David Holmes
David.Holmes at oracle.com
Mon Apr 25 23:23:43 PDT 2011
I have a query regarding the warnings that javac now issues that are
related to the work on multi-catch that does more precise analysis of
the exceptions that can be thrown. Here's an example:
../../../src/share/classes/java/util/concurrent/ThreadPoolExecutor.java:1115:
warning: unreachable catch clause
} catch (Throwable x) {
^
thrown types RuntimeException,Error have already been caught
and here's the code:
try {
task.run();
} catch (RuntimeException x) {
thrown = x; throw x;
} catch (Error x) {
thrown = x; throw x;
} catch (Throwable x) {
thrown = x; throw new Error(x);
} finally {
afterExecute(task, thrown);
}
This pattern is not uncommon. The final catch of Throwable is there to
close the gap that exists because of the API's (Class.newInstance) that
allow you to throw checked-exceptions from methods that don't declare
them. So the catch clause is not in fact unreachable.
It seems to me that these warnings will encourage people to remove the
"offending" catch clause and actually end up with code that is not
robust because of these historical flaws in the API's (not to mention
native calls).
Perhaps it would be better to restrict the warnings to the cases where a
previous clause will catch the exception expected by the later clause,
rather than incorrectly assuming which exceptions can be thrown from the
methods in the try-block.
David
More information about the coin-dev
mailing list