aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

xie li
Ranch Hand

Joined: Nov 30, 2005
Posts: 54
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?
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
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
Navjeet Nehra
Greenhorn

Joined: Sep 18, 2003
Posts: 13
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

Joined: Sep 30, 2005
Posts: 63
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

Joined: Sep 30, 2005
Posts: 63
Hey sorry, I think i should have said that the return output in the catch is commented.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception