my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and exception" Watch "Overriding and exception" New topic
Author

Overriding and exception

Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1558
Overriding method can not throw broader exception... does it apply only to "checked" exceptions??


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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)


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
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

Joined: Nov 01, 2005
Posts: 86

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding and exception