• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception ??

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is one of the Q in the IBM Test
To prevent code which follows a try block from being executed when an exception is caught, you can:
a) None of the above.
b) Code a return statement in the finally clause.
c) Call System.exit() in the catch clause.
d) Code a return statement in the catch clause.
Select all correct ans
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say answer is C, D. The reason I am saying this is, according to the question, there is a try block and an exception is caught. So, this makes it clear that there is a catch block too. Also remember that if you don't have a catch or finally block but just the try block, the compiler will complain, if I am correct (try without catch or finally?? or some error of this kind). Hence, answers C and D look correct to me.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's wrong with alternative B? The finally block is always executed and therefore a return statement in the finally clause would also do the trick.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bergling,
I agree ,C and D are right.
see this code regarding option B:

when line //a is uncommented, the compiler gives an error saying line not reached.
it means you can't have a return inside finally?
Ramani.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks,
I think the correct answer is only C.
Firstly I would interpret "code which follows a try block" to include both catch and finally blocks. Remember there can be just try-finally block without the catch block??
The return statement in catch block will not prevent the finally block from being executed!. The return value is stored in some temporary variable, the finally gets executed and then the value is returned. Point to remember is, finally block gets executed no matter what.
Sytem.exit() is the only statement which is capable of altering this behaviour. Take a look at the small program here which I wrote to help you understand the concept.

As always, if there is anything you don't understand here, or if you want to contest my conclusions, feel free to wake me up
Hope this helps.
Ajith
[This message has been edited by Ajith Kallambella (edited September 22, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
well, whatever ajith said, seems to be OK!!
only system.exit() can cause NOT to execute finally block.
(if there is NO exception in finally block itself)
 
Viji Bharat
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All:
Well, interesting responses. But, I re-read the question carefully and can't help but say why shouldn't the correct answer be A (none of the above). The reason I am saying is in option C, it is NOT mentioned that System.exit is the first line in the catch block. So, isn't it possible that it may have some other statements before System.exit and this might get executed?? Which means, it doesn't exactly prevent code following try block from being executed! Ajith and others, if I haven't confused you, please give your input. However, if we assume that System.exit is the first statement, then I agree with Ajith in saying only answer C is correct.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Viji..
A is not the right answer. How can it be?? Read it again- it says "None of the above" and there is nothing above A!!... just kidding
Well, I have to agree with you. Your observations are right indeed. System.exit() does not prevent statements preceeding it from being executed. The question is vaguely worded and I only hope that you don't face such confusing questions in the exam. My answer was probably 'biased' from my previous experience. I have seen similar questions in other mock exams and I tried to read the question(with missing words!) right
Anyway I hope this discussion helped others understand the significance of System.exit() in try-catch-finally construct
Good luck,
Ajith
reply
    Bookmark Topic Watch Topic
  • New Topic