File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception ??" Watch "Exception ??" New topic
Author

Exception ??

Rahul
Greenhorn

Joined: Sep 22, 2000
Posts: 6
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


Njoy
Viji Bharat
Ranch Hand

Joined: Sep 18, 2000
Posts: 101
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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.
ramani2020
Greenhorn

Joined: Sep 14, 2000
Posts: 28
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.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Sep 18, 2000
Posts: 101
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

Joined: Mar 17, 2000
Posts: 5782
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception ??
 
Similar Threads
Exceptions
try block
try catch block
Exceptions
why exception is not thrown