Bug 98892

Summary: [compiler] Eclipse compiler generates code that calls the finally block twice
Product: [Eclipse Project] JDT Reporter: Tushar Pokle <tushar.pokle>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: CLOSED FIXED QA Contact:
Severity: critical    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1 RC2   
Hardware: All   
OS: All   
Whiteboard:

Description Tushar Pokle CLA 2005-06-08 04:30:22 EDT
The following code executes differently depending on whether it is compiled
using the Eclipse compiler (3.1 RC1), or Sun's javac.

public class TooManyTimes {

    public static void main(String[] args) {
        new InnerClass().start();
    }

    public static class InnerClass {
        public Object start() {
            System.err.println("So long!");
            try {
                return null;
            } finally {
                System.err.println("And thanks for all the fish!");
                foo();
            }
        }

        private void foo() {
            throw new IllegalStateException("Gah!");
        }
    }

}


The output generated when compiled with eclipse is:
So long!
And thanks for all the fish!
And thanks for all the fish!
Exception in thread "main" java.lang.IllegalStateException: Gah!
	at TooManyTimes$InnerClass.foo(TooManyTimes.java:19)
	at TooManyTimes$InnerClass.start(TooManyTimes.java:14)
	at TooManyTimes.main(TooManyTimes.java:4)




The output when compiled with javac is:
So long!
And thanks for all the fish!
Exception in thread "main" java.lang.IllegalStateException: Gah!
        at TooManyTimes$InnerClass.foo(TooManyTimes.java:19)
        at TooManyTimes$InnerClass.start(TooManyTimes.java:14)
        at TooManyTimes.main(TooManyTimes.java:4)




The difference is that "And thanks for all the fish!" is printed twice for the
eclipse compiled version :-) The finally clause is executed twice!!!
Comment 1 Philipe Mulet CLA 2005-06-08 11:58:25 EDT
Oops ! Thanks for finding this.
Comment 2 Philipe Mulet CLA 2005-06-08 12:37:01 EDT
This only occurs when performing JSR inline (like in compliant 1.5 mode).
Comment 3 Philipe Mulet CLA 2005-06-08 12:39:16 EDT
Problem comes from an exception attribute range which comprises the inlined
finally block, thus in case of exception, it gets trapped and activates the any
exception handler (second invocation of the finally block).
Comment 4 Philipe Mulet CLA 2005-06-08 13:07:47 EDT
Added TryStatementTest#test027-028.

Fixed by exit/entering subroutine code when performing inlining of finally block
at exit locations in code.

Fixed
Comment 5 Tushar Pokle CLA 2005-06-08 20:27:56 EDT
Thanks Philippe! Good work :-)
Comment 6 Olivier Thomann CLA 2005-06-09 09:33:52 EDT
Output is now:
So long!
And thanks for all the fish!
Exception in thread "main" java.lang.IllegalStateException: Gah!
	at TooManyTimes$InnerClass.foo(TooManyTimes.java:19)
	at TooManyTimes$InnerClass.start(TooManyTimes.java:14)
	at TooManyTimes.main(TooManyTimes.java:4)

Verified with N20050609-0010 + JDT/Core HEAD
Comment 7 David Audel CLA 2005-06-10 11:15:06 EDT
Verified for 3.1 RC2 using build I20050610-0010