aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Exceptions" Watch "Question about Exceptions" New topic
Author

Question about Exceptions

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
The following code is a question from K&B's book.



Which, inserted independently at // insert code here, will compile, and produce the output b?

A. String doFileStuff() { return "b"; }
B. String doFileStuff() throws IOException { return "b"; }
C. String doFileStuff(int x) throws IOException { return "b"; }
D. String doFileStuff() throws FileNotFoundException { return "b"; }
E. String doFileStuff() throws NumberFormatException { return "b"; }
F. String doFileStuff() throws NumberFormatException,
FileNotFoundException { return "b"; }

In my first attempt I chose "D" but the correct answers are "A","D","E","F".

Can anyone give me some more detais about the correct answers ?

Would be highly appreciated if you could also clarify me the following related questions :

1 - Supposing that doFileStuff() method was declared in an interface and throws an IOException, is the concrete implementer class obligated to throw the exception too ?

Can a concrete class implement an interface or extend a super class (abstract or not) by adding more exceptions than declared in super class or interface ?


"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Hi Bessa,



A. String doFileStuff() { return "b"; }
B. String doFileStuff() throws IOException { return "b"; }
C. String doFileStuff(int x) throws IOException { return "b"; }
D. String doFileStuff() throws FileNotFoundException { return "b"; }
E. String doFileStuff() throws NumberFormatException { return "b"; }
F. String doFileStuff() throws NumberFormatException,



A, D, E, F are quite correct for this problem. As overriding states that the new method which is overridden, should
1. NOT throw new Checked exceptions (So B and C are excluded from solution set)
2. May throw any RuntimeExceptions or their subclasses (As NumberFormatException is a subclass of RuntimeException, E & F are part of solution set)
3. May declare the same Exceptions or their subclasses or subset or none
(This brings A & D into the solution set)

So the solution set has A, D, E & F. Hope this satisfies you.

Regards,
Narendranath
[ February 12, 2006: Message edited by: Naren Chivukula ]

Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Thank U Naren,

The problem in this question is that I didn't know NumberFormatException is a subclass from RuntimeException. So, assuming that you are correct.

Thanks a lot.
jane Zhan
Greenhorn

Joined: Feb 27, 2006
Posts: 3
Originally posted by Naren Chivukula:
Hi Bessa,




A, D, E, F are quite correct for this problem. As overriding states that the new method which is overridden, should
1. NOT throw new Checked exceptions (So B and C are excluded from solution set)
2. May throw any RuntimeExceptions or their subclasses (As NumberFormatException is a subclass of RuntimeException, E & F are part of solution set)
3. May declare the same Exceptions or their subclasses or subset or none
(This brings A & D into the solution set)

So the solution set has A, D, E & F. Hope this satisfies you.

Regards,
Narendranath

[ February 12, 2006: Message edited by: Naren Chivukula ]


C should be correct too. It is a case of overloading instead of overriding. Can someone please confirm?
Jason Liao
Ranch Hand

Joined: Jun 17, 2005
Posts: 59
C is not correct. It can pass the compilation, but not output "B".
 
 
subject: Question about Exceptions
 
Similar Threads
K&S Chapter 5 q-3
K&B question - overriding methods
Exception handling question K&B
mock question about exceptions...from K&B SCJP book
Exceptions doubt