Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused

 
kavita s. kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
my question is
overridden method must not throw any exception which is not
thrown by overridden method
is it true..
i feel overriden method should not throw any checked exception which is not thrown by overridden method
i am right
thanks in advance
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kavita s. kumar:
Hi
my question is
overridden method must not throw any exception which is not
thrown by overridden method
is it true..
i feel overriden method should not throw any checked exception which is not thrown by overridden method
i am right
thanks in advance


Kavitha
If you override a method
The overriding method must throw nuthing/same-exceptions/subclass of the exceptions thrown by the original method
HIH
Ragu
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kavita,
I think you are right:

class A {
void m() {
System.out.println("A");
}
}
public class E extends A {
void m() throws ArrayIndexOutOfBoundsException {
System.out.println("B");
}
}
The class E compiles without problem.
So, the overriding method can throw any "unchecked" exceptions. Perhaps this is also one reason for which the some exceptions are "checked", becouse the compiler "checks" them.
..Cristian
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic