• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Jxam Exceptions question  RSS feed

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sgwbutcher,
I Agree with you but I suppose you would like to say C and E instead of D and E.
Adrian
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!