File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding exception and the overriding

 
sai panindra
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was that a question to me or Marco ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco. Sorry, I opened the thread and posted a reply and didn't realise you were posting simultaneously.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem! But I really was confused for a moment
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic