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

Exceptions.

 
Nilesh Raje
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends..!

I have a confusion here..!

Suppose We have a Interface in which there are methods that throws a Exception .Now in the implementation of that method do we have to throw the same exception or can we throw the subclass of that exception or vice-versa.

Thanks in advance..!


(Ed topic: No need to shout!)
[ August 30, 2005: Message edited by: Barry Gaunt ]
 
Satish SN
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nilesh,

If u declared a particular Exception for the method in the Interface
it is not compulsory in the first place to throw any Exception in the implementation class.

If u want to throw exception for the method in the implementation class as u said it should be a subclass of the exception defined in the Interface

my observation showed me that if throw ArithmeticException in the Interface in the implementation class i throwed for the same method FileNotFoundException so with this i understood that it is not at all mandatory to throw the same exception in the implementation class.

another observation is if i throwed "Exception" in the implementation class as it was superclass it gave compile time exception.
 
Nilesh Raje
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Satish,

Thanks afor your reply..!But when Exceptions with respect to Inheritance,overiding..! We cannot throw the subclass of the exception in the overiding method or we cant throw more genralized for of exception in the overiding method ..!We have to throw exactly or more specific exception..!

Well then as per your previous mail y is it not so in Case of Interfaces..!
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Added to Nilesh answer.
I have learnt that the method in a class which implements an interface, can't throw any new checked exception other than the one declared in the interface method.

Eg:
interface A{
public void test() throws IOException;
}

class B implements A{
public void test() throws IOException {

}
}
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry ya!!
I pasted the eg wrongly

Eg;
interface A{

public void test() throws IOException;

}

class B implemets A{

public void test() throws SQLException{
//some code
}

}

This eg will give complie time error, because the implementation calss throws the new checked exception.
 
Nilesh Raje
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sangeetha,

Well thats what i think but then Satish has a contradictory answer here..! This is leading to confusion..!Whats the rite one?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than guessing or "feeling", read what the Java Language Specification Section 8.4.6 says.
 
Kalyana Sundaram
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
Just have a glimpse on the example below. This would clear most of your doubts. Please note that all the are public classes and organized in a package.


I hope this better explains the concepts. Please post further if this is not understandable.

^GudLuck^
 
sangeetha balasubramaniam
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got this info from K & B book

key points

1.Maintain the signature of the interface method,and maintain the same return type(but does not have to declare the exceptions declared in the interface method declaration).

2.must not declare any new checked exception for an implementation method.

3.must not declare any checked exception that are broader than the exception declared in the interface method.

4. May declare any runtime exceptions on any interface method implementation regardless of the interface declaration.

Do work on some example pgm, you will get it right, its useful rather than discussing...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic