Bug 98892 - [compiler] Eclipse compiler generates code that calls the finally block twice
Summary: [compiler] Eclipse compiler generates code that calls the finally block twice
Status: CLOSED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: All All
: P3 critical (vote)
Target Milestone: 3.1 RC2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-06-08 04:30 EDT by Tushar Pokle CLA
Modified: 2005-06-10 11:15 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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