File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on subclass exception 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 "Question on subclass exception" Watch "Question on subclass exception" New topic

Question on subclass exception

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Scenario 1

output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Exception IOException is not compatible with throws clause in Test1.doit()

at Testp.doit(
at Testp.main(

Scenario 2

output : hi

I have override the metho doit() in both scenario.
As far i know that if superclass decalres the Exception , the n subclass must or not delcare the exception. If it declares then it should declare the subtype of exception.

Here in these 2 scenarios my subclass declares the exception,
Scenario 1 throws me the runtime/complie error.
Scenario2 doesn't throws any exception.

Please explain i am getting confused..

Thanks<br />Dinesh
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
subclass must declare exception or sublass of this exception .... but this is only true for checked exceptions (exceptions that derive from Exception but not from RuntimeException).
RuntimeException and it's derivates are 'unchecked exceptions'. Method may declare unchecked exception, but compiler doesn't care about it.
Error and Throwable are unchecked too.

[ June 23, 2008: Message edited by: Ireneusz Kordal ]
[ June 23, 2008: Message edited by: Ireneusz Kordal ]
I agree. Here's the link:
subject: Question on subclass exception
It's not a secret anymore!