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
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.
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).]
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Joined: Nov 22, 2008
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)
Joined: Sep 18, 2000
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.
Joined: Mar 17, 2000
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