Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error caught with Exception-Handler

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both Exception and Error inherit from Throwable
(meaning they are at the same level)
why does the compiler allow you to catch
a subclass of Error using an Exception "catch handler"?






I believe the answer is:
Because Error is not a "checked exception".


Please confirm
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LinkageError IS-A Error which is an unchecked exception, only subclasses of Exception (which are not a subclass of RuntimeException) are checked exceptions.

The catch handler is not catching the error, illustrated with this code snippet:The output is exactly the same as in your original post (exc is not printed).

Hope it helps!
Kind regards,
Roel
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel.

The catch block using the "Exception" Handler is not entered.
The code will compile but the Error will not be caught by the Exception Handler.
()()()
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:The catch block using the "Exception" Handler is not entered.
The code will compile but the Error will not be caught by the Exception Handler.

Indeed!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic