JDK-4908268 : Complier generates warning if you throw an exception inside a finally block
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-08-18
  • Updated: 2003-09-05
  • Resolved: 2003-09-05
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 08/18/2003


FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If you explicitly throw an exception inside a finally block, you get a compiler warning: "warning: finally clause cannot complete normally". The code still executes properly, but the warning seems incorrect, or misleading at best.
You do not get the warning if you call a method that throws an exception.


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile the given code sample.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No warnings or errors.
ACTUAL -
foo.java:9: warning: finally clause cannot complete normally
        }
        ^
1 warning

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class foo {
    public static void main(String[] args) throws Exception {
        try {
            System.out.println( "...in try" );
        } catch (Exception e) {
        } finally {
            System.out.println( "...in finally" );
            throw new Exception("Oops");
        }
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Instead of explicitly throwing an exception, you can call a method that throws it instead.
(Incident Review ID: 199613) 
======================================================================

Comments
PUBLIC COMMENTS ...
10-06-2004

EVALUATION The compiler is correct, however the warning should be optional and disabled by default. ###@###.### 2003-09-05
05-09-2003