• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception handling  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"i know finally block cannot modify primitive values."

What makes you think this statement is true? Where did you get that information from?
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know finally block cannot modify primitive values.


I don't think this is correct, and I believe your program demonstrates that it's not.
 
Ranch Foreman
Posts: 3071
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"if i remove comment it returns 10 else it returns 20..why is this??"

Stick a sysout in the finally block after changing returnVal.
With that earlier line uncommented the flow should be obvious. 'Return the value of returnVal in catch block', then 'change the value of returnVal in finally block', then 'exit'.
Essentially the value to be returned from the catch block has already been put to one side, before the finally is executed, so the value returned isn't changed, but 'returnVal' is.

With the line commented out the flow is 'change returnVal in finally block' then 'return the value of returnVal'.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also Akshay: Please DontWriteLongLines - it makes your post very hard to read. I've broken yours up this time.

In fact, it's actually much better if you post your question outside your code.

Winston
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
'Return the value of returnVal in catch block', then 'change the value of returnVal in finally block', then 'exit'.

With the line commented out the flow is 'change returnVal in finally block' then 'return the value of returnVal'.


your two sentence has solved my entire doubt ..thanks...very precise explanation..
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Also Akshay: Please DontWriteLongLines - it makes your post very hard to read. I've broken yours up this time.

In fact, it's actually much better if you post your question outside your code.

Winston


your advice have been taken into consideration. I will be taking are of that
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:"i know finally block cannot modify primitive values."

What makes you think this statement is true? Where did you get that information from?


actually i referred to exam tip in Mala Gupta's izo-803 .. it states that

EXAM TIP Watch out for code that returns a value from the catch block and
modifies it in the finally block. If a catch block returns a primitive data
type, the finally block can’t modify the value being returned by it. If a catch
block returns an object, the finally block can modify the value being
returned by it.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see where you may have got confused.

Finally blocks of code are no different to code written elsewhere. However, the purpose is normally to free up resources. As of Java 7 however, you can use try-with-resources statements: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html. They handle the freeing up of resources for you automatically.
 
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something peculiar about that quote.

If you go back to your code, you have a local variable returnVal, 10. Now if an Exception occurs, control passes to the catch (line 18) and the method completes abruptly. You can read about abrupt completion in these two threads: 1 2 Make sure to read them. Also have a look at the Java Language Specification link in those threads.
As I said, if an Exception occurs and control passes to the catch, the method completes abruptly and no code after it is executed.
But what about the finally? Surely a finally is always executed irrespective of the state of the rest of the method? Well, it is, but you can't see it. If you put a print statement into the finally like this you can see it:-
finally
{
   returnVal += 10;
   System.out.println("In finally: returnVal = " + returnVal);
}

… you will see that the value of 20 is indeed present.

So what is this about primitives? That is an effect of pass‑by‑value. As you know Java supports pass‑by‑value, pass‑by‑value and pass‑by‑value. Never pass‑by‑reference.
So, if you return the value of a primitive or the address of an object reference, no other code can alter that. If you return 10 or the address of the String "Akshay Rawal" that is what is returned. No other use of the = operator (remember += uses the = operator indirectly, and ++ has a similar effect) can alter that value. You can write = 999 or = "Campbell Ritchie" 1000000 times and nothing will happen to the original value returned. That value is safely stored in wherever it has been returned to. So no code could alter a value previously returned. [Code in a finally is the only code which is ever executed after a return.]

What it means is that a mutable reference type can be accessed and its state can be changed. You can read about it in Effective Java by Joshua Bloch. Look at this codeThe catch returns a reference to a Kettle which it has heated by 60°. Then the finally cools the Kettle down. The calling method will receive a reference to a hot Kettle which immediately turns into a cold Kettle.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason my post appeared with a couple of lines missing so I have posted it again and deleted the original version. I think writing [Code caused the text to disappear.
 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
<snip very thorough explanation>


Definitely more detailed than my cobbled together post, made from sticky tape, washing up bottles and string.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But we were explaining different things. You explained why you get 10 or 20. I explained why that Mala Gupta quote was confusing.
 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But we were explaining different things. You explained why you get 10 or 20. I explained why that Mala Gupta quote was confusing.


I completely missed your first sentence for some reason.

Your description seemed to cover the stuff I was talking about (the order in which things are done)...performance testing in mushing my mind, clearly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!