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 Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

Balaji Sadasivam
Greenhorn

Joined: Nov 16, 2000
Posts: 25
I would like to know the possible answers for the following question:
Suppose a MyException should be thrown if Condition() is true,which statements do you have to insert?
1. public aMethod {
2.
3. if (Condition) {
4.
5. }
6.
7. }
Select all that apply.
1. throw new Exception() at Line 4
2. throws new MyException() at Line 4
3. throw new MyException() at Line 6
4. throws new Exception() at line 2
5. throws MyException at Line 1

Thanks
Regards
Balaji
Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
I'd say 3&5.
naveen sahu
Ranch Hand

Joined: Nov 15, 2000
Posts: 48
Originally posted by Balaji Sadasivam:
I would like to know the possible answers for the following question:
Suppose a MyException should be thrown if Condition() is true,which statements do you have to insert?
1. public aMethod {
2.
3. if (Condition) {
4.
5. }
6.
7. }
Select all that apply.
1. throw new Exception() at Line 4
2. throws new MyException() at Line 4
3. throw new MyException() at Line 6
4. throws new Exception() at line 2
5. throws MyException at Line 1

Thanks
Regards
Balaji

if MyException is the name of the Exception Class rather then the Exception Object itself , then the correct choice is 2.
because on condition satisfy basis , only statements can be inserted at line no 4 , no where else.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
If a method is declared to be throwing an exception, you can do one of the following
1. Enclose the method call in a try-catch block and catch the exception that is being thrown.
2. Forget about try-catch safety and just declare the calling method itself to be throwing the same exception.
It is perfectly legal you can do both of these.
So the answer is (5)
5. throws MyException at Line 1
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Balaji Sadasivam
Greenhorn

Joined: Nov 16, 2000
Posts: 25
Thanks for your reply Michael.
I selected the same options in the practice exam. But I didn't get any credit for my answer. The question is from Jxam.
Is the answers are wrong ??
Thanks
Regards
Balaji
Balaji Sadasivam
Greenhorn

Joined: Nov 16, 2000
Posts: 25
Thanks Ajit and naveen.
I tried the question with your answers (option 2 ONLY and option 5 ONLY).
Still it says it is wrong..
I am confused...
Regards
Balaji
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
2 is most definitely wrong, you can't use throws in a method, only throw. SO the correct answer should be 3 & 5.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Except 3 is wrong because you are supposed to throw the exception only if the condition is true. This is the correct answer:
1. public aMethod throws MyException {
2.
3. if (Condition) {
4. throw new MyException();
5. }
6.
7. }
So only 5 is a valid choice. There should be a choice that says: throw new MyException() at Line 4


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
I agree with Thomas Paul. Answer 3 would mean that the exception is thrown regardless of Condition. Answer 5 has to be correct.
Balaji Sadasivam
Greenhorn

Joined: Nov 16, 2000
Posts: 25
Thanks guys.
Special thanks to Ajit and Thomas for the detailed explanation.
Regards
Balaji

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling
 
Similar Threads
Jxam Quation
throwing Exceptions
Clarification on Jxam questions
Jxam Exceptions question
Jxam question