• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
young ,are you chinese?
i am
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
It will give me the powers of the gods. Not bad for a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic