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 Regarding Overiding 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 "Regarding Overiding" Watch "Regarding Overiding" New topic
Author

Regarding Overiding

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
If the overridden method does not throw exceptions of any type,
then can the overriding method in subclass throw any new exceptions ?
Susie Chow
Ranch Hand

Joined: Jun 12, 2001
Posts: 33
No, you can only throw the subclass of the exception that the overridden method throws.
Susie Chow
Ranch Hand

Joined: Jun 12, 2001
Posts: 33
However, I have just come across with this question. There is no compiling error when compiling the following code. RuntimeExcpetion is not a subclass of ClassNotFoundException. When I change RuntimeException to SQLException (they are all subclasses of Exception), it doesn't compile. Can anyone explain to me?
Thank you.
public class Base
{
public void aMethod() throws ClassNotFoundException{}
}
public class Derive extends Base
{
public void aMethod() throws RuntimeException{}
}
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
As far as i know, the overidding method( derived class method ) can throw subclasses of the exceptions or subset or all the exceptions
thrown by the overidden method( base class method ). Runtime exceptions can be
thrown by the overiding method even if the overidden method
does not throw them.
Also i can quote two points :
1. RuntimeException is a unchecked exception. So the overriding methods can throw unchecked exceptions even if the overidden method does not throw them
2. As with your replace RunTimeException in code of derived class with SQLException,
which is a checked exception, it is not a subclass of ClassNotFoundException, so code does not compile.
If i change the below code as
import java.sql.*;
class Base
{
public void aMethod() throws ClassNotFoundException, SQLException{}
}
class Derive extends Base
{
public void aMethod() throws SQLException{}
}
Code compiles fine as SQLException is one of the checked
exceptions thrown by Base class.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Susie
The compiler doesn't check for runtime exceptions it only check checked exceptions. SQLException is a checked exception.

hope that helps
Dave


Dave
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Is there a comprehensive lists of checked exceptions and unchecked exceptions?
Susie Chow
Ranch Hand

Joined: Jun 12, 2001
Posts: 33
Check the API http://java.sun.com/j2se/1.3/docs/api/index.html
Checked exceptions are generally java.lang.Exception where runtimeException is a subclass of Exception. I think unchecked exception refers to java.lang.Error? Correct me if i am wrong. Thank you.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Susie,
RuntimeExceptions are any exceptions that subclass RuntimeException. They can be thrown at any point as the compiler never 'checks' for them; which is why they don't cause a compile error if the listed in an overriding methods 'throws' clause. Checked exceptions subclass Exception vs RuntimeException.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Overiding