• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Exception

 
Ranch Hand
Posts: 54
  • 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: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
Since the method type is void it would not return any output to the main method and therefore your program compiles and returns no output.

However if you try to modify the code as follows:


public class Test {
public static String output ="";

public static String foo(int i) {
try{
if(i==1) {
throw new Exception();
}
output += "1";
}
catch(Exception e) {
output += "2";
// return;

}
finally {
output += "3";
}
output += "4";
return output;
}
public static void main(String args[]) {
foo(0);
System.out.println("The output after foo(0) is:"+output);
foo(1);
System.out.println("The output after foo(1) is:"+output);
}
}


then the output after foo(0) would be "134" and output after foo(1) would be "134234".

think thats what you need, pardon me if im not to the point you expect..

regards
Balaji.S
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure that the last output would be "234" since the last line in the catch block is return;

I think the value of the output should be "13423"
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After finally block is executed it should go to line output+="4" right..

So it concatenates 4 to the output too.
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey sorry, I think i should have said that the return output in the catch is commented.
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic