• Post Reply Bookmark Topic Watch Topic
  • New Topic

try/catch/finally block  RSS feed

 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi im new to exception handling..here in class ''A'' i want to find res?
whether exception raised or not finally executes.. that i know..
so though 30 is ans..but giving 2 as answer?
Could any one explain it please...




 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it would help if you would be a little more specific..i.e. tell us EXACTLY what the out put is. I ran it and see this:

C:\slop>java A
main begin
0
2
30
main result: 2
main complete

The code hits line 23 and says "I need to return a...that's currently 2. Got it." Then the finally block runs. a gets set to 30, and that gets prints. THEN the value we had earlier is returned to the calling method...the '2'. And tha'ts what prints.
 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fred,
Thanks for your quick reply.
here my question is why m i getting res value 2. instead of 30. even though try blcok returning value 2 .it (a) is reassigning to 30 in finally block.so res value must be 30.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's like Fred said. The code returns 2 (not a, but 2). Then it sets a to 30, which has no effect on the return value which was already set.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding return a; as the last line in the finally statement.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you have "return a", java doesn't return the memory location that a refers to...it returns the value in that memory at the moment you hit that line. When you hit line 23, a's memory location holds the value 2. Java remembers the "2".

you then enter your finally block. the finally block changes the value in that memory location, but not the value it is holding onto that it will send back to the calling method.

Think of it this way..you leave your house, and come into mine. In my house, you see a piece of paper with the value 2 on it. you remember the number 2. You start to leave my house. After you are done looking at that paper, I erase the 2 and write 30 on it. You get home, you say "I need to write down the number I saw on that piece of paper". What would you write down? You never saw me make my change, so you write down 2.
 
srinivas sy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks allot fred..i understand it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!