• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

could some one explain how the control flows on the following code..

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the result of attempting to compile and run the program?

a. Prints: 0,0,0,1,0,0
b. Prints: 0,0,1,1,0,1
c. Prints: 0,1,1,1,0,1
d. Prints: 1,0,1,1,0,1
e. Prints: 1,1,1,1,0,1
f. Compile-time error
g. Run-time error
h. None of the above


Answer is b.

thanks in advance,
reena


EDIT by mw: Added Code Tags to original poster's indentation. (Please use Code Tags.)
[ August 14, 2006: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your interpretation of how this would execute? What exception is thrown? Where is it caught? Etc...
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what happening in the above code:

At the inner try block, the value of x is 1 therefore 'Level1Exception' will be thrown in case 1.
The catch is only for Level2Exception therefore it won't enter there.
The 'finally(c++)' will be executed and the value of c = 1
The outer catch(Level1Exception) will catch the above exception and thereby increments the values of d. d =1.
Outer finally(g++) makes g=1.

Therefore the result will be 0,0,1,1,0,1
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing to remember here Reena is that Level1Exception IS-NOT-A Level2Exception, so the catch block for Level2Exception will not be run. BUT, the finally blocks will always run, no matter what.

If the catch block for Level1Exception was not there, then the catch block for Exception would have been run, as Level1Exception IS-A Exception. And, as before, the finally block always runs.
 
R .sourav nayak
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys for the reply. Krishnan your explaination was really helpful.
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may find that a useful tool to answer questions like this is to run the code through a debugger such as the ones built into NetBeans or Eclipse. The debuggers are possibly the most userful features of these tools, and for most learning purposes I recommend the naked JDK.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic