aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on subclass exception" Watch "Question on subclass exception" New topic
Author

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(Testp.java:14)
at Testp.main(Testp.java:23)

*********************
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: http://aspose.com/file-tools
 
subject: Question on subclass exception
 
Similar Threads
Getting duplicate method error
Exceptions and interfaces
catching exception
Problem in understanding how JVM triggers Main method
:rolling: Innovative functions !!!