Article View: borland.public.jbuilder.java.language
Article #16394finally and return
From: Doug Chamberlin
Date: Fri, 26 Feb 1999 00:00
Date: Fri, 26 Feb 1999 00:00
62 lines
2057 bytes
2057 bytes
I just discovered what I feel is a strange behavior. I was debugging a
Java program which had a "return true" statement inside a finally
block of a method. The effect of this statement was to CANCEL then
exception. That is, the method returned normally and did not propigate
the thrown exception which was active upward in the call stack.
Anyone else see this behavior? it was totally unexpected by me!
Here is an example:
package untitled1;
public class MyClass1 {
private int finalResult = 0;
public static void main(String argv[]) {
new MyClass1();
}//main
public MyClass1() {
try {
finalResult = 1;
finalResult = catchesException();
finalResult = 2;
}//try
catch (Exception e) {
finalResult = 3;
}//catch
finally {
System.out.println("finalResult is "+finalResult);
}//finally
}//MyClass1
private int catchesException() throws Exception {
int thisResult = 0;
try {
thisResult = 4;
thisResult = throwsException();
thisResult = 5;
return thisResult;
}//try
catch (Exception e) {
thisResult = 6;
throw e;
}//catch
finally {
System.out.println("thisResult is "+thisResult);
return thisResult; //This is the offending statement
}//finally
}//catchesException
private int throwsException() throws Exception {
throw new Exception("This is the exception.");
}//throwsException
}
What I get as output here is:
thisResult is 6
finalResult is 2
Do you think this is what should happen?
Technical Details
Message-ID:
<36d6e3bb.6330119@forums.inprise.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,92c7634c9c470034,start X-Google-Attributes: gidf74b3,public From: dchamberlinNOSPAM@dlawton.com (Doug Chamberlin) Subject: finally and return Date: 1999/02/26 Message-ID: <36d6e3bb.6330119@forums.inprise.com> X-Deja-AN: 448526534 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Organization: D. Lawton Associates Mime-Version: 1.0 Reply-To: dchamberlinNOSPAM@dlawton.com Newsgroups: borland.public.jbuilder.java.language