Reducing "exception is never thrown in body of corresponding try statement" Error to a Warning
Alex Buckley
alex.buckley at oracle.com
Wed Nov 6 00:48:31 UTC 2019
This kind of substantial change to the Java language demands inspection
from all angles. Here's one. Suppose a beginning programmer writes this:
void m() {
try {
throw new CheckedException1();
} catch (CheckedException2 e) {
log(e);
}
}
Both CheckedException1.class and CheckedException2.class are legal
exception types, but the `2` in the catch clause is a typo.
Today the program doesn't compile, and the error is clear:
"CheckedException2 is never thrown in body of corresponding try
statement". Independently, there is another error: that
"CheckedException1 must be caught or declared to be thrown". One way or
another, the typo will be corrected.
In your proposal, the first error is replaced with a warning -- in some
sense taking the position that CheckedException1 is unchecked in the try
statement because you don't NEED to catch it there -- but the second
error will still occur, because CheckedException1 is checked in the
method body as a whole. The programmer dutifully adds a 'throws' clause
to the method declaration; the error goes away; the programmer is happy;
the world is worse off.
Alex
On 11/5/2019 3:29 PM, Mike Duigou wrote:
> Hello again;
>
> Hearing no specific objections to my prior suggestion
> (https://mail.openjdk.java.net/pipermail/jdk-dev/2019-October/003480.html)
> to tweak compilation of checked exceptions by reducing the "exception is
> never thrown in body of corresponding try statement" compilation error
> to a non-fatal warning I am going to proceed with creating a JIRA issue
> and proceeding with proposing a changeset to the compiler-dev list.
>
> I do agree with the sentiment that this change is of somewhat limited
> value. It is, I believe, useful and incrementally an improvement upon
> existing behaviour and worth pursuing in absence of grander proposals.
>
> Cheers,
>
>
> Mike
More information about the jdk-dev
mailing list