aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaring Broader Checked Exceptions when implementing an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declaring Broader Checked Exceptions when implementing an interface" Watch "Declaring Broader Checked Exceptions when implementing an interface" New topic

Declaring Broader Checked Exceptions when implementing an interface

Trey Carroll

Joined: May 06, 2008
Posts: 17
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
public void test() throws FileNotFoundException{

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


Trey Carroll<br />======================<br />Studying for the SCJP
Ankit Garg

Joined: Aug 03, 2008
Posts: 9327

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();

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Trey Carroll

Joined: May 06, 2008
Posts: 17
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.
Sam Aran

Joined: Sep 06, 2008
Posts: 12
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

Joined: May 06, 2008
Posts: 17
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.
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58

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;


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???
rocket mass heater kickstarter
subject: Declaring Broader Checked Exceptions when implementing an interface