Having started in a job where I am to write Java code, I am working my
way through the Java Language Specification. But the following
situation gives me problems:
public class Foo {
public static void foo(boolean b) {
try {
System.out.prin tln(b);
if( b ) throw new Exception();
} finally { return ; }
}
public static void main(String[] args) {
foo(true); foo(false);
}
}
This code is safe at runtime (because the finally block silently
discards the exception when it terminates abruptly), but as far as I
can read the JLS, it should lead to a compile-time error. However, the
code is happily accepted by all of Sun JDK 1.5, Eclipse 3.2 JDK and
Jikes 1.22.
Specifically, in the third edition of JLS, section 14.18 about the
throw statement says:
| .. at least one of the following three conditions must be true, or a
| compile-time error occurs:
| * The exception is not a checked exception [...]
| * The throw statement i contained in the try block of a try
| statement and the type of the Expression is assignable to the
| type of at least one catch clause [...]
| * The throw statement is contained in a method or constructor
| and the type of the Expression is assignable to at least one
| type listed in the throws clause of the declaration.
Neither of those three conditions are true for my code, so why am I
not getting the compile-time error the text promises me? Am I
overlooking some relevant general clause somewhere, and if so which?
One compiler that does produce a compile-time error is gcj, so if I'm
wrong at least I'm not alone...
--
Henning Makholm "Jeg har tydeligt gjort opmærksom på, at man ved at
følge den vej kun bliver gennemsnitligt ca. 48 år gammel,
og at man sætter sin sociale situation ganske overstyr og, så
vidt jeg kan overskue, dør i dybeste ulykkelighed og elendighed."
way through the Java Language Specification. But the following
situation gives me problems:
public class Foo {
public static void foo(boolean b) {
try {
System.out.prin tln(b);
if( b ) throw new Exception();
} finally { return ; }
}
public static void main(String[] args) {
foo(true); foo(false);
}
}
This code is safe at runtime (because the finally block silently
discards the exception when it terminates abruptly), but as far as I
can read the JLS, it should lead to a compile-time error. However, the
code is happily accepted by all of Sun JDK 1.5, Eclipse 3.2 JDK and
Jikes 1.22.
Specifically, in the third edition of JLS, section 14.18 about the
throw statement says:
| .. at least one of the following three conditions must be true, or a
| compile-time error occurs:
| * The exception is not a checked exception [...]
| * The throw statement i contained in the try block of a try
| statement and the type of the Expression is assignable to the
| type of at least one catch clause [...]
| * The throw statement is contained in a method or constructor
| and the type of the Expression is assignable to at least one
| type listed in the throws clause of the declaration.
Neither of those three conditions are true for my code, so why am I
not getting the compile-time error the text promises me? Am I
overlooking some relevant general clause somewhere, and if so which?
One compiler that does produce a compile-time error is gcj, so if I'm
wrong at least I'm not alone...
--
Henning Makholm "Jeg har tydeligt gjort opmærksom på, at man ved at
følge den vej kun bliver gennemsnitligt ca. 48 år gammel,
og at man sætter sin sociale situation ganske overstyr og, så
vidt jeg kan overskue, dør i dybeste ulykkelighed og elendighed."
Comment