wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes regarding  exception and the overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regarding  exception and the overriding" Watch "regarding  exception and the overriding" New topic
Author

regarding exception and the overriding

sai panindra
Greenhorn

Joined: Jan 06, 2009
Posts: 5
class A{
public void test(){System.out.print("test");}}
class B extends A{
public void test() throws RuntimeException{
if(true) throw new RuntimeException();
}

The above two classes compile fine. But when I replace RuntimeException with IOException , it throws a compilation error saying that overridden method test does not throw IOException. Then how does it work with the Runtime Exception?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi,

that's a difference between unchecked exceptions like RuntimeExceptions (or subtypes of it) and checked exceptions like IOException!

If a parent class declares to throw a checked exception, subclasses basically have to throw at least the same checked exceptions to fulfill the contract of the parent class.

Marco
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Marco Ehrentreich wrote:If a parent class declares to throw a checked exception, subclasses basically have to throw at least the same checked exceptions to fulfill the contract of the parent class.


The other way round. A child class method does not have to be declared to throw any exception, but it can't be declared to throw a checked exception that the parent class method is not declared to throw.

gives the error
ExceptionMethod() in test.ExceptionChild2 cannot override ExceptionMethod() in test.ExceptionParent; overridden method does not throw org.xml.sax.SAXException at line 23, column 0

Joanne
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Oh sorry, I didn't look carefully enough at the source code. I just thought the inheritance in the example was the other way around.

Thanks for clarification Joanne!


Marco
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Is that the right way round? Surely a subclass (overridden) method is a refinement of the superclass method. So in every condition under which the superclass method executes, the subclass method must execute too. So a subclass (overridden) method must not declare any more exceptions than the superclass method.


Of course, if you declare an unchecked exception, the compiler overlooks the declaration.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Was that a question to me or Marco ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Marco. Sorry, I opened the thread and posted a reply and didn't realise you were posting simultaneously.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

No problem! But I really was confused for a moment
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Marco Ehrentreich wrote:No problem! But I really was confused for a moment


Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Joanne Neal wrote:Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer
Both.

And it gives you something to wind me up about. What I do is open several threads, read one, close it, read the second, close it, so you might have ½ hour to post before I even notice there is a reply.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Campbell Ritchie wrote:
Joanne Neal wrote:Campbell has a very loose definition of the word simultaneously. Either that or he's a very slow typer
Both.

And it gives you something to wind me up about. What I do is open several threads, read one, close it, read the second, close it, so you might have ½ hour to post before I even notice there is a reply.


You should know that it's only we women who can multitask.
You just stick to doing one thing at a time and your life will be so much easier
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Joanne Neal wrote:You should know that it's only we women who can multitask.
You just stick to doing one thing at a time and your life will be so much easier
But that will spoil things for you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding exception and the overriding