• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Exception Handling

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Had a question about the following code:


Prints " seal, seal, 3 " when compiled and run. I was wondering how is this code getting compiled cause try block in aboat method is returning 1 and then in the finally block, there is return value of 3. Isnt this wrong cause a method can return only one value and even if it can return 2 values, why 1 is not printed??

Regards
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why 3 is printed instead of 1 is that the finally block is run even though 1 was supposed to be returned in the try block. Since the finally block does a return statement as well, this return statement takes precedence over the return statement in the try block.

This would also be the case if the catch block was entered and the return 2 statement was reached. The finally block would run and its return statement would overrule the one in the catch block.

Remember: The finally block always runs unless System.exit() is called or something happens like turning off the computer.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood what is told but plz tell me why

class TooColdException extends RuntimeException{ }
public class Lsmul{

public static void main(String argv[]){
Lsmul lsm = new Lsmul();
lsm.go();
System.out.println(lsm.aboat());
}

public void go(){
nfl();
}

public int aboat(){
int a;
try{
nfl();
a=5;
return a;
}
catch(TooColdException tce){
System.out.println(tce.getMessage());
return 2;
}
finally
{
a=6;

}
}
public void nfl() throws TooColdException{
System.out.println("seal");
}
}


The output of this program is seal, seal and 5 if the finally is executed the value returned should be 6.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to output the value of 'a' in the finally block, and you'll understand that this variable is different from the one you declared in aboat();
The finally block actually acts a bit like a subroutine, with its own local variables, which will be called to each exiting points.
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya i have tried printing a value but i want to know that the scope of a is local to the method aboat so it should be reflected while returning.
 
gaurav singhal
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Plz explain me what do mean by finally block act as a subroutine with its own local variable because the variable it is changing is defined in function level.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaworld.com/javaworld/jw-02-1997/jw-02-hood.html
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


See above. Value is 5 because its not object reference you are returning but primitive. a is already returned before the finally block is run. If you put return a in finally block then it returns 6.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic