aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Over-riding method which throws exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Over-riding method which throws exception" Watch "Over-riding method which throws exception" New topic
Author

Over-riding method which throws exception

Ravi Chandanani
Greenhorn

Joined: May 31, 2011
Posts: 4
Hello All,

I have one query regarding following example,




1:import java.io.FileNotFoundException;
2:import java.io.IOException;

3:class SubException extends Exception { }
4:class SubSubException extends SubException { }

5:public class CC { void doStuff() throws SubException { } }

6:class CC2 extends CC { void doStuff() throws SubSubException { } }

7:class CC3 extends CC { void doStuff() throws Exception { } }

8:class CC4 extends CC { void doStuff(int x) throws Exception { } }

9:class CC5 extends CC { void doStuff() { } }

10:class CC6 extends CC { void doStuff() throws FileNotFoundException { } }



here if we compile above example,there will be compilation error at line 7 & 10.

according to the rule "An overriding method cannot throw a broader exception than the method it's
overriding."


For line number 8 the exception thrown is "Exception" it self,which is not broader than class SubException,as class SubException already extending the class Exception.

can anyone explain what is the reason for compilation error at line 7 & 10.


Thanks in advance,
Ravi
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Hi Ravi,
the rule is:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method.

Line 7 cause a compile error, because it declare broader exception:
SubException is subclass of Exception, so Exception is broader.

Line 10 cause a compile error based on rule I wrote above: Overriding method cannot throw new or broader exception.
Method doStuff() on line 10 declare that it throws FileNotFoundException, which is totally different and new kind of exception.

Line 8 is OK, because it is not overriding. Because of doStuff(int x) has a parameter, it is overloading instead of overriding.

|OCPJP 6|
matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
matej spac wrote:Hi Ravi,
the rule is:

The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method.

Line 7 cause a compile error, because it declare broader exception:
SubException is subclass of Exception, so Exception is broader.

Line 10 cause a compile error based on rule I wrote above: Overriding method cannot throw new or broader exception.
Method doStuff() on line 10 declare that it throws FileNotFoundException, which is totally different and new kind of exception.

Line 8 is OK, because it is not overriding. Because of doStuff(int x) has a parameter, it is overloading instead of overriding.
Andreas Svenkson
Ranch Hand

Joined: Jan 17, 2011
Posts: 179
Like matej said. It seems perhaps you have misinterpreted the rule in this case - you cannot throw exceptions which are HIGHER up in the inheritance tree than the one declared in the original class. You can however throw exceptions which are LOWER.

The reason should be obvious; any reference to a subclass should possibly be available through a reference of the original class, thus meaning any exceptions their methods throw must be convertable to the exception in the original class - hence the exceptions they throw must be lower in the inheritance hierarchy.

If class CC throws SubException, and you have an object of CC3, you can reference this object via a CC-reference variable. But that means the exceptions from the object must match, or be convertable to, the exceptions of the reference.

// Andreas
Ravi Chandanani
Greenhorn

Joined: May 31, 2011
Posts: 4
Hi matej spac,


Thanks for quick reply,

My interpretation from following statement,
"The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method"
is if class C extends class B,class B extends class A,class A extend Exception.

The class C is broader than class B and class B is broader than class A and class A is broader than class Exception.

If i am correct here,then class SubException would be broader class than class Exception.

class SubException extends Exception { }
public class CC { void doStuff() throws SubException { } }
class CC3 extends CC { void doStuff() throws Exception { } }




Please correct me if i am miss understanding anything here,

Thanks,
Ravi Chandanani

matej spac
Greenhorn

Joined: Apr 21, 2011
Posts: 28
Ravi Chandanani wrote:
is if class C extends class B,class B extends class A,class A extend Exception.

The class C is broader than class B and class B is broader than class A and class A is broader than class Exception.

If i am correct here,then class SubException would be broader class than class Exception.


Exactly opposite :-)

Look at this class tree:



Each sub and subsub and ... exception describes exception in more detail - so each exception above is broader - it describes broader area of possible exceptions.
For example, UnknowHostException describes some exception in detail, but its parent class - RemoteException describes broader area of exceptions ... and IOException is even broader because it describes some kind of broader area of exceptions... Exception class - at the top of hierarchy is the broadest in this example.
Ravi Chandanani
Greenhorn

Joined: May 31, 2011
Posts: 4
Hello matej spac ,

Yes thats true,I am now clear about "broader and nerrow" concept,

Thanks you so much for quick reply,
Ravi Chandanani
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Over-riding method which throws exception