• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about Exceptions

 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 577
Tomcat Server Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is not correct. It can pass the compilation, but not output "B".
 
please buy this thing and then I get a fat cut of the action:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic