Community
Participate
Working Groups
Build 20020412 from Rodrigo Peretti: The following code (from org.eclipse.core.internal.boot.InternalBootLoader) does not compile with javac (I've tried with IBM 1.3, 1.3.1 and Sun's 1.4 beta). It complains that Throwable should be caught or should be in the throws clause. It compiles fine with the JDT compiler. Any ideas? Rodrigo /** * @see BootLoader */ public static Object run(String applicationName/*R1.0 compatibility*/, URL pluginPathLocation/*R1.0 compatibility*/, String location, String[] args) throws Exception { Object result = null; applicationR10 = applicationName; // for R1.0 compatibility String[] applicationArgs = null; try { applicationArgs = startup(pluginPathLocation, location, args); } catch (Exception e) { throw e; } String application = getCurrentPlatformConfiguration ().getApplicationIdentifier(); IPlatformRunnable runnable = getRunnable(application); if (runnable == null) throw new IllegalArgumentException("Application not found: " + application); try { result = runnable.run(applicationArgs); } catch (Throwable e) { e.printStackTrace(); throw e; } finally { shutdown(); return result; } }
Simpler test case: [package p1; public class X { public Object run(Object result) throws Exception { try { System.out.println("aaa"); } catch (Throwable e) { throw e; } finally { return result; } } }]
Javac 1.2.2 did also accept this code.
The reason why we accept this code is that the offending code actually never ends up throwing the Throwable exception. It is intercepted by the finally block which instead will return some value. So in practice, the offending exception doesn't need to be caught.
JLS 14.19.2 precisely describe this situation: 14.19.2 Execution of try-catch-finally A try statement with a finally block is executed by first executing the try block. Then there is a choice: ... If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice: ... If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten). --------- In the original test case, the exception thrown within the catch block isn't handled inside this try statement, but the finally block makes it irrelevant. javac has a bug.
*** Bug 14818 has been marked as a duplicate of this bug. ***