Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Overiding

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can only throw the subclass of the exception that the overridden method throws.
 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Susie
The compiler doesn't check for runtime exceptions it only check checked exceptions. SQLException is a checked exception.

hope that helps
Dave
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a comprehensive lists of checked exceptions and unchecked exceptions?
 
Susie Chow
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic