Community
Participate
Working Groups
The following code is compiled without a hitch by eclipse 2.0.2 and M4. The unhandled Exception is not detected. package misc; /** * @author ext-toni.nykanen * * Eclipse 2.0.2. w/ jdk 1.3.1. (the one that comes with bea wls 6.1.) * compiles this nicely. * * When executed from the command line with jdk 1.3.1. javac, the compiler * errors. */ public class ExTest { private static String foo() { try { System.out.println("Toimii"); throw new Exception(); } finally { return null; } } public static void main(String[] args) { foo(); } }
JDK1.4.1 and jikes 1.18 compile this code as well. I would say this is a bug in JDK1.3.1. The exception is not thrown. It is handled in the finally block and the result of the method foo is always null.
Closing, as Olivier said, this code is perfectly legite by the language spec.
Yes. This is indeed a bug in 1.3.1. Checked from jls later. (Should have done it first.)
*** Bug 34301 has been marked as a duplicate of this bug. ***
*** Bug 40979 has been marked as a duplicate of this bug. ***
I am using Sun JDK 1.4.1 build 1.4.1_02-b06, it gave me the error: C:\testjava>java -version java version "1.4.1_02" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06) Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode) C:\testjava>javac * ExTest.java:4: unreported exception java.lang.Exception; must be caught or decla red to be thrown try { System.out.println("Toimii"); throw new Exception(); } ^ 1 error for the same piece of code posted.
I just tried. And I get: D:\temp>java -version java version "1.4.1_02" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06) Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode) D:\temp>cat ExTest.java public class ExTest { private static String foo() { try { System.out.println("Toimii"); throw new Exception(); } finally { return null; } } public static void main(String[] args) { foo(); } } D:\temp>javac ExTest.java D:\temp> So I don't understand what is happening on your machine. What is sure is that this was a bug in javac. The exception doesn't need to be handled in your code because it is never thrown. The return point of your method is the return statement in the finally block. A finally block is always executed. If the finally block doesn't return (no return/throw statement inside the finally block), then the exception needs to be handled, because it will be rethrown at the end of the subroutine.