aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions." Watch "Exceptions." New topic
Author

Exceptions.

Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
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 ]

Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
Satish SN
Ranch Hand

Joined: Apr 19, 2005
Posts: 70
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.


Satish SN<br />SCJP 1.4 & SCWCD 1.4
Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
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

Joined: May 16, 2005
Posts: 23
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

Joined: May 16, 2005
Posts: 23
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

Joined: Aug 02, 2005
Posts: 153
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

Joined: Aug 03, 2002
Posts: 7729
Rather than guessing or "feeling", read what the Java Language Specification Section 8.4.6 says.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kalyana Sundaram
Ranch Hand

Joined: Mar 18, 2005
Posts: 94
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^


Only those who will risk going too far can possibly find out how far one can go !!!
sangeetha balasubramaniam
Greenhorn

Joined: May 16, 2005
Posts: 23
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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exceptions.