Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no exception thrown in class A. How is it possible to instantiate the class A inside the try.. catch. I expected a error, "there is no exception thrown in class A". Can anyone clarify this?

[ June 19, 2002: Message edited by: Thiru Thangavelu ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that your catch block catches "Exceptions". It doesn't catch IOException. One of the descendents of Exception is RuntimeException. As RuntimeExceptions need not be declared, you are free to try to catch them at any time. If you change that catch block to catch an IOException, I'll bet you'll get the compiler error you expected.
Corey
[ June 19, 2002: Message edited by: Corey McGlone ]
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even this compiles fine how?
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks as always Corey,
that cleared my doubt.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception is the only checked exception that can be declared within a catch clause without being thrown.
 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about Throwable?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thiru Thangavelu:
how about Throwable?

Only descendents of Exception (with the exclusion of RuntimeException) are considered checked exceptions. Anything else (RuntimeException and anything higher on the inheritance hierarchy) is not checked.
Corey
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, Error is a subclass of java.lang.Throwable, and we're not required to check for errors, only checked exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic