This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes throw exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "throw exception question" Watch "throw exception question" New topic
Author

throw exception question

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
In K & B book, when it talks about Exception class, there are 2 examples that have compiler error, but it did not say what the problem is:






Can someone help me which line in these two examples has the problem, and how to correct them?

Thanks


SCJA, SCJP5.0, SCBCD, SCWCD
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by reubin nibuer:




The doStuff() method throws a checked exception, but does not declare the exception in a throws clause. It would compile if you changed the method declaration to:
void doStuff() throws MyException {






The method someMethod() invokes doStuff(), which throws a checked exception. Therefore, one of two changes could be made to the code to allow it to compile.
(1) The declaration of someMethod() could be changed to
void someMethod () throws MyException {
OR
(2) Inside someMethod(), the call to doStuff() could be put inside a try-catch block that would handle the MyException.


SCJA 1.0 (98%), SCJP 1.4 (98%)
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
I got it, thank you very much
 
jQuery in Action, 2nd edition
 
subject: throw exception question