File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance with Exception thrown declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance with Exception thrown declaration" Watch "Inheritance with Exception thrown declaration" New topic
Author

Inheritance with Exception thrown declaration

Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
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

Joined: Jun 28, 2000
Posts: 477
Hi! Anybody there?
hemanshow
Greenhorn

Joined: Jun 26, 2000
Posts: 25
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

Joined: Jun 28, 2000
Posts: 477
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

Joined: May 31, 2000
Posts: 116
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

Joined: Jun 28, 2000
Posts: 477
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

Joined: Jun 28, 2000
Posts: 477

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.
 
 
subject: Inheritance with Exception thrown declaration
 
Similar Threads
How to throw exception in run method
JMS Error
B&S: Database file
current-element-node property not recognized
ServletException()