• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finally abrupt return

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question on a Mock exam for SCJP1.4:




I think the answer should be A1 but its A4. I think its A1 bcos even though return exits finally block abruptly,it still compiles without error.

What do you guys think ?
Also do you think there would be any difference in answer,if compiled with 1.4 or 1.5 ?
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would give the same compile (error) output for both 1.4 and 1.5.

The method must have 'throws ' in its signature, because the compiler sees that it is possible that an exception will be propagated up the chain.
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try the program .I ran it ,its output is 3 .No compiler error
I ran on version 5.I dont think it should made any difference
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is running without adding any throws.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above code runs without any error but if you omit the finally block then you 'll get a compiler error and the exception needs to be declared. Can somebody explain this behaviour.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program compiles but with a warning: finally block doesn't complete normally . Executing a return or throw in the finally clause abruptly completes the finally clause which, in turn, abruptly completes the try statement and has the effect of masking/overriding any previous return or throw statement executed in the associated try/catch blocks.

Also, if the return statement is commented then we would have to specify the throws clause which confirms that the return statement in finally is overriding try/catch clauses.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I ran above program, it compiles fine & output is 3. But when I change program like below... I moved return statement from finally to try, now it gives me compiler error "Unhandled exception" at line 1. Can any one please explain what is happening here?

 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Also I got unreachable statement at line 2. That I understand.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B you cant throw a checked exception from a catch block unless you handle AND declare it. However the orignal example in this thread seems to be voilating this rule because the method is able to throw a checked exception from the catch clause without declaring it.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because there is a return statement in the finally block.

Normally, when an exception is thrown that is about to be thrown out of the method (it isn't handled by a catch-block), the finally-block is executed and then the method ends with the exception being thrown.

However, if you return from the finally-block, then you cut short the normal way of operation. Because of the return statement, the exception is swallowed and the method ends as if it returns normally. The exception does not need to be declared in a throws-clause, because it is never really thrown out of the method - the return statement prevents that.

In general, it's practically never a good idea to put a return statement inside a finally-block.
[ September 19, 2007: Message edited by: Jesper Young ]
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unreachable code-Error is casued by first throw statement in try {}
2nd throw in catch produces unhandled Exception error even if there is finally block.

if finally{
return 0;} //then it runs
but if
finally{} //error

even i m not getting this behaviour
catch runs if try throws exception ,catch passes to finally.
finally doesnt handle exccpetion...actually.but why error not coming in some cases ,some cases coming
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper, that explains it all
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to compile following code and it forces me to write a return statement in finally block only. why???

I don't understand the behavior. The normal convention is to write return statement in try block. Please explain.

The error is: missing return statement.


public class Test33 {

public static void main(String args[]){
System.out.println(method());
}
public static int method() {
try{
return 3;

}
catch(Exception e){
//throw new Exception();

}
finally{


}
}
}
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code
________________________________________________________________________
public class Test33 {

public static void main(String args[]) {
System.out.println(method());
}

public static int method() {
try {
return 3;

} catch (Exception e) {
// throw new Exception();

} finally {

}
}
}
______________________________________________________________________

When, method has return type as well as try block, this is necessary that you define a catch block or finally block with reurn statement or last statement of method should be return statement. Duo to this, compiler ensure about return.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
I tried to compile following code and it forces me to write a return statement in finally block only. why???


No, it does not force you to write a return statement inside the finally block. You could also put the return statement after the finally block. Your method returns an int, so every possible path in the code that leads to the end of the method, must end in a return statement that returns an integer value.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUOTE]No, it does not force you to write a return statement inside the finally block. You could also put the return statement after the finally block. Your method returns an int, so every possible path in the code that leads to the end of the method, must end in a return statement that returns an integer value

whats the use to put return statement after finally,anyway what i feel is , that return will never get executed,




i think line 4 will never get executed , but still it executes fine and my prog ends in finally without any return.

Actually after finally , its going to end in try block , so i edited this post. Ignore my comment please
[ September 20, 2007: Message edited by: Gunjan Kumar ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have modified the code to look like this...

public class Test3 {
public static void main(String args[]){
method1();
System.out.println(method());
try{
throw new Exception();
}
catch(Exception e){

throw new Exception(); //A

}
finally{
}
}
public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception(); //B
}
finally{return 3;}
}
public static void method1(){
try{
throw new Exception();

}
catch(Exception e){
throw new Exception(); //C

}
finally{return;}
}

}

A:
Now as per me it is giving error in A because it does not have any method to propogate this exception to (main being at the bottom of the stack)

B & C: It wont give any error because ...
.... ""First off, we know that, by definition of the try/catch/finally statement (Section 14.19 of the JLS), the finally block is executed no matter what happens in the try/catch blocks.
Next, also by definition, both the return (Section 14.16) and throw (Section 14.17) statements complete abruptly.
Lastly, by definition, abrupt completion of a finally block (Section 14.19.2) causes the try/catch/finally statement to complete abruptly.
Therefore, executing a return or throw in the finally clause abruptly completes the finally clause which, in turn, abruptly completes the try statement which therefore has the effect of masking/overriding any previous return or throw statement executed in the associated try/catch blocks.
So, take a look at your try/catch/finally statements and check to see that you're actually handling exceptions and returns the way that you expected. " ...for more see..http://www.jguru.com/faq/view.jsp?EID=288261

Hope it helps

[ September 20, 2007: Message edited by: Abhishek khare ]
[ September 20, 2007: Message edited by: Abhishek khare ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic