Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Catching exception question

 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, if try to catch something that cannot be thrown from anywhere, I'll have an unreachable code and an compile error. For instance, this code gives me a compile error.


I understand this fine. But when you don't use Exception subclasses, the following code compiles fine. In other words, if you try to catch Exception base class, it's fine, it doesn't generate compile errors.


Can anyone clarify this behaviour for me?
 
Adam Czysciak
Ranch Hand
Posts: 90
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Leandro Melo:
In other words, if you try to catch Exception base class, it's fine, it doesn't generate compile errors.


Can anyone clarify this behaviour for me?[/QB]


Sure ;-) You can catch any unchecked exceptions at any time. RuntimeException and it's subclasses are instanceof Exception, therefore you can catch it. The same applies to Error and its subclasses. And, obiously, you could also try to catch Throwable.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure! Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic