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
posted
0
I'd say 3&5.
naveen sahu
Ranch Hand
Joined: Nov 15, 2000
Posts: 48
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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