• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance with Exception thrown declaration

 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody describe me the rules about the signature for inheriting methods that throws exceptions and for inherinting methods that doesn�t thrown any Exception but in the new method you want to throw one, I think this second case is illegal. I�m trying code for deducing this, but I get confused.
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Anybody there?
 
hemanshow
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First make sure that the method is actually over-riding and not over-loading.
Now rule is:
Over-riding method can throw :
1.no exception.
2.exception thrown by over-ridden method.
3.any subclass of exception thrown by over-ridden method.
right?
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Hemanshow for your reply, but I think that the rules are a bit different for constructors, do you know?
[This message has been edited by Marcela Blei (edited July 21, 2000).]
 
rajsim
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For constructors, the rule is opposite.
Generally, constructor must have a throws clause indicates
that it throws same or more exceptions than the super-class
constructor (that it calls implicitly or explicitly).
Same rule applies if the constructor calls another constructor
in the same class using "this(...)".
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rajsim.
I made the following code
public class OverloadException {
/*
Hierachy Exceptions:
Throwable
Exception
IOException
EOFEXception
*/
OverloadException() throws java.io.IOException {}
//I am overloading the constructor for checking all cases in the same code
OverloadException(int i) throws java.io.IOException {}

public void withException() throws java.io.IOException {}
public void withException2() throws java.io.IOException {}
class OverloadExceptionDaughter extends OverloadException {
//If you don t declare a constructor a compiler throws an error
//Override with a Parent Exception works!
OverloadExceptionDaughter() throws Exception {}
//Override with the same exception an others work fine
OverloadExceptionDaughter(int i) throws java.io.IOException, java.io.EOFException {}

//Rule on methods
//Signature without exception works fine
public void withException(){}
//Rule on methods
//Signature with a child Exception works fine
public void withException2() throws java.io.EOFException {
throw new java.io.EOFException();}
}
}
 
Marcela Blei
Ranch Hand
Posts: 477
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lets take a view to the rules (correct me please if something is wrong)
1 - When overriding methods, the throws Exception clause of the overriding class may throw:
a) No exception
b) The same exception
c) A subclass of the exception thrown in the parent method
It can�t throw:
a) An exception that doesn�t follow the below rules (parent exception of the exception thrown in the parent method or an exception that doesn�t belong to the parent exception hierachy)
2 - When overriding constructors that throws exceptions:
a) The overriding class must throw the same exception or a parent of the exception thrown in the parent constructor.
b) If a) is done, the overring constructor can throw another exceptions that doesn�t need to belong to the parent exception hierachy.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic