Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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?
 
bibby young
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From my point of view it should be
"134234".
After the first invocation,it's "134",because the "finally" block always runs.
And line 23 adds "234" to the string.
Please correct me if any mistake!!
[ December 18, 2005: Message edited by: bibby young ]
 
Navjeet Nehra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the last line in the catch block is return. The output would include the last "4"
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
young ,are you chinese?
i am
 
Sudhir palavalasa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output would be "13423". The "4" at last will NOT be included, because of the "return" statement in the catch block.Only finally block will be executed after "return".Please check.

Regards,
Sudhir
 
xie li
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic