This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

Wats the output..... Explanations needed....

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
1. public class Test {
2. public static String output ="";
3.
4. public static void foo(int i) {
5. try {
6. if(i==1) {
7. throw new Exception();
8. }
9. output += "1";
10. }
11. catch(Exception e) {
12. output += "2";
13. return;
14. }
15. finally {
16. output += "3";
17. }
18. output += "4";
19. }
20.
21. public static void main(String args[]) {
22. foo(0);
23. foo(1);
24.
25. }
26. }
What is the value of the variable output at line 23?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output = "134" before calling foo(1);
output = "13423" after calling foo(1);
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't you get a compile error? I thought you could'nt have code in between a try{} and catch{} block.

Or is it ok since all the code between try and catch in this instance is in the same scope?
[ June 16, 2006: Message edited by: Wally Valters ]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wally, if u give a closer look to the code you will see that the statement is inside Try block. So there's no problem


Regards
swapnil
[ June 16, 2006: Message edited by: Swapnil Trivedi ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When foo(0) is executed, the output value is "134", since catch block is not executed.
When main method calls the foo(1) method, the output value continues with the "134" value, since output is static string variable.Only one copy of output is created.
When foo(1) is executed, the output value is "1342", since in the catch block, there is a return statement. 3 and 4 values cannot be appended to the output variable. Here, return statement stops the execution of finally and outside the finally block code and returns to the main method.

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).

As per my knowledge, the above analysis is correct.
If there is any mistake, sorry for the reply.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by srilatha rao:

So, the value of output variable is "134" before calling foo(1) and "1342" after calling foo(1).



What about finally. finally will always be executed even at second time.

Output given by Wise is correct.



Naseem
[ June 16, 2006: Message edited by: Naseem Khan ]
 
srilatha rao
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I am sorry, yes, the output is "13423".
finally will be executed in the second time also.
Thanks
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output is 13423
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all!
tell me srilathawhy the statement afeter finally block not execute
i understand upto 13423 but why not 4 i mean why not 134234
please help me
 
Shiaber Shaam
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bajaj...
if there occurs a return stmt in catch or finally block... the code in finally gets executed and the code after finally gets skipped.... I hope it will make sense....
 
krish bajaj
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello shiaber

just try this program without using return statement in catch block then after finally block statement execute easily
Do not use number simply use write statement for understanding
i did ur program and somewhere my concept is clear
if u like my answer then well and good otherwise sorry because i am new player in this field
 
It's exactly the same and completely different as this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic