Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Exceptions

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is not correct. It can pass the compilation, but not output "B".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic