• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Over-riding method which throws exception

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello matej spac ,

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

Thanks you so much for quick reply,
Ravi Chandanani
 
reply
    Bookmark Topic Watch Topic
  • New Topic