• Post Reply Bookmark Topic Watch Topic
  • New Topic

return in catch clause  RSS feed

 
Duc Ta
Ranch Hand
Posts: 46
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
What is the risky when I return a particular value in catch clause of try/catch block?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you draw a diagram with paths of execution on. Write return abc; in the try and return xyz; in the catch. Then you can see what will happen if you do or do not suffer a caught Exception.
Note that failure to write return xyz; in the catch may cause the compiler to complain that not all paths of execution cause a value to be returned. So it may be correct to wrute return xyz; in the catch.
Now do the same with a finally block and you shou‍ld be able to see why it is usually wrong to return anything from a finally.
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether you return something in catch block or not . No matter finally block will always exceute. OK

So first of all if you return something in catch block then control goes to finally and then goes directly to main. Flow dosen't goes to code after try-catch-finally block but goes to main directly.

See for clarity->




See Output is->  In Try Block
                         In Catch Block
                         In Finally Block
                         Flow comes to main  //Note here flow goes from finally to main directly and not to the code after try-catch-finally block due to return statement in catch block.
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try above code once and then comment the return statement in catch clause and then see the magic........
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference would there be if you returned a value from those blocks? The OP was asking about returning “a particular value”.
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so the difference in returning values from catch block is->

CASE 1->if you return a primitive from catch block then finally block dosen't modify the value of that primitive . See->



Its Output is->

In Try Block a= 20
In Catch Block a= 20
Inside Finally a is 10
In main value of a is 20

CASE 2-> if you return an object from catch block then finally block can modify the value of that object . See->




Its Output is->

In Try Block sb= HELLO
In Catch Block sb= HELLO
Inside Finally sb is HELLO BYE
In main value of a is HELLO BYE

 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have misunderstood both what the OP wanted and you aren't clear about modifying the value of an object. What I think you want is something like this:-I have tried to correct your indentation, mostly by removing the excess empty lines. The value you saw is the old value of a, a sort of backwards pass by value.
Now, when you return a reference type, you are keeping the reference to the same StringBuilder object unchanged. It is always the same object. But by the time you reach the finally, you have changed its state. What you are printing with the first println statement is the old state of an object, and the last println statement prints the same object, but its new state. You would see that better with this sort of thing:-
System.out.printf("StringBuilder object: %s %<h%n", sb);
You shou‍ld see from the hash code printed that you are dealing with the same object throughout. Remember that StringBuilder doesn't override Object#hashCode().
Your comment that objects are passed by reference is wrong. Everything is passed by value Full stop.
But the OP wanted to see what happens if you return different values from try and catch.>
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for correcting me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!