IntelliJ Java IDE
The moose likes Mock Exam Errata and the fly likes Jxam Exceptions question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "Jxam Exceptions question" Watch "Jxam Exceptions question" New topic
Author

Jxam Exceptions question

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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: }
Ans:
A. throw new Exception() at line 4
B. throws new MyException() at line 4
C. throw new MyException() at line 6
D. throws new Exception() at line 2
E. throws MyException at line 1

I don't know which one to choose ... But the ans. is "B" and "E". But the answer given is "B" and "E". I guess this might be a typo he meant to say "throw new MyException() at line 4"
Is there any one to agree with ...?
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
Dear Surya,
Assuming that MyException is extends a checked exception, then in the absence of exception handling code in the method (try-catch-finally), you must both throw the exception and let others who call the method know it throws the exception so both B and E are required.
The correct code would be:
1: public void aMethod throws MyException {
2:
3: if (Condition) {
4: throw new MyException();
5: }
6:
7: }

[note: method is missing a return type which I added as "void."]
Best regards,
Steve Butcher

-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi sgwbutcher,
Thanks for your reply. To through an exception I thing we use "throw" keyword instead "throws" .
Isn't it???
Pls. clarify my doubt.
Thanks
Surya
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Steve's code is correct. You use "throws" in a declaration to say that a method or constructor may throw an exception; you use "throw" in the body of the method or constructor to actually cause the exception to be thrown. The two cases are different.


"I'm not back." - Bill Harding, Twister
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Sgwbutcher,
I Agree with you but I suppose you would like to say C and E instead of D and E.
Adrian
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Hi Adrian,
Check the first line of the example - "Suppose a MyException should be thrown if Condition() is true". Exception will be thrown only when condition() evaluates to True and control will be transferred to line 4 and hence the option B is correct.
Regards,
Milind

[This message has been edited by Milind Kulkarni (edited June 27, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Sorry, I didn't read the original question carefully. Steve's code is correct, but the answers provided are not, as Surya noticed in his first post. B and E are the closest possible answers in this case - E is correct but only half the answer; B is almost the other half, but it has a typo - it should be "throw" not "throws". (And also a return type is necessary for aMethod(), as Steve pointed out.) So just answer B and E, and I'll move this to Errata.
Nitin Dabadghav
Greenhorn

Joined: Jun 28, 2000
Posts: 13
According to me the answer is A & E.
At line 4 throw new Exception() should be added.
else
Modify the method at line 1. append the following line to the method declaration.
throws Exception.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
Both B and A are close to the right answer. B says "throws" when it should say "throw"; A says "Exception" when it should say "MyException" (that is clearly what the problem requires). It seems to me that B is "closer" to the correct answer than A - but either way, the question in incorrect as written. Neither A nor B is really correct - that's why this is in Errata. The question is broken.
 
 
subject: Jxam Exceptions question
 
Threads others viewed
Exception Handling
Jxam question
MyException
Jxam Quation
Clarification on Jxam questions
developer file tools