File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on subclass exception

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic