• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and exception

 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding method can not throw broader exception... does it apply only to "checked" exceptions??
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont remember right now, but To make it simple for you, just remember this sentence

"An overriding method can throw any number/none exceptions provided they
are same/sub classes of thos thrown by the original method."
(dont remember rule about the checked exception,check the K&B book)
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A { void method() {}}

class B extends A {
void method() throws RuntimeException {
throw new RuntimeException(); //an overriding method can throw
//unchecked exception not thrown by the
//original method
}
}
 
Purujit Saha
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Akhil Trivedi

Overriding method can not throw broader exception... does it apply only to "checked" exceptions??



Hi you are right. Overridden Methods can not throw any Broader Exceptions.Overridden methods can throw only that checked exception or its sub class Exception that the original methods throws.

And its is applicable to only checked exception not any Runtime Exception.
Since,if a method in super class does not throw any exception then the sub class overridden method can't have any checked exception but can have any runtime exception in its throws clause.

Hope this help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic