• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaring Broader Checked Exceptions when implementing an interface

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I'm having trouble understanding something. K&B 1.5 pg 70 says:

A legal non-abstract implementing class has the following properties... It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.



To check my understanding of this statement I created an interface Testable:


and a class, Test:



My intent was to prove to myself that I understood the statement. Since FileNotFoundException is a subclass of IOException (and therefore a narrower exception), I predicted that the code would compile. I was wrong. I get the following compile error:

test() in pkg1.Test cannot implement test() in pkg1.Testable; overridden method does not throw java.io.FileNotFoundException
public void test() throws FileNotFoundException{



Can someone please help me to understand what's going on here?

TIA
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well actually the compiler is able to deduce that the code in the method test, is not throwing FileNotFoundException in any case.

try this

class Test implements Testable{

public void test() throws FileNotFoundException{
System.out.println("In test();");
throw new FileNotFoundException();
}
}
 
Trey Carroll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help on this Ankit. I have inserted the statement that you suggested:


throw new FileNotFoundException();



but I still can't get it to compile.

I'm afraid that the trouble may be that I am interpreting "narrower" incorrectly. I *think* that what is supposed to be true is that the method in the implementing class can declare new CheckedExceptions provided that they are "narrower", that is, a subclass of the CheckedException declared by the method declared in the interface.

If anyone can tell me what is wrong with this I will be very grateful.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Sun's Java compiler? I'm using the Java 6 compiler (windows 32-bit) and I'm not having any problems compiling your code. I copied the code verbatim, and even somewhat-autogenerated another version using a different package through eclipse (called test for the package, and I can compile it fine).

On the command line, I had to use this line to compile both files, though:


Does the code compile with an IDE such as netbeans or eclipse? I've found that generally it's easier to check compilation with an IDE...
 
Trey Carroll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Samus. I got it to compile.

I feel embarrassed for posting this, but your reply really helped me to track down the problem: I wasn't recompiling the latest version of the Testable Interface.

I like working with JCreator's new>file for creating "toy" apps to test concepts as I read K&B, but the lack of a project really got me on this one. I could see the class file in /pkg1. I just didn't realize that it was for a version of Testable that didn't declare any Exceptions! Thanks again. I was completely baffled.

For anyone following this thread, the code that I posted was good; I was making a stupid error. You CAN throw new CheckedExceptions when you implement an interface, provided that the new CheckedException is a subclass of an Exception that the method in the interface declares. The idea of a narrower exception corresponds to an Exception that is a subclass.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Here I am little bit confused.

I have made the same example like I have created one java program in which I have defined interface and class in the same file as shown below:

package javaranch;

import java.io.FileNotFoundException;
import java.io.IOException;


interface Testable{
void test() throws IOException;
}
public class Sample implements Testable{

public static void main(String args[]){

}

public void test() throws FileNotFoundException {
}
}


My this code is not showing any compile time errors.

So, what is your error is of???
reply
    Bookmark Topic Watch Topic
  • New Topic