Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

runtime exceptions thrown by overridden functions

 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this code:




and the following code that creates and calls the above...




WHY is this alllowed to compile? The overriden function in class Derived is specifying an exception (NumberFormatException) that is not specified/declared by the Base class. From my understanding it is allowed to compile because NumberFormatException is a runtime exception and not a checked exception, correct?

How do you know, in general,if "xxxException" is a runtime exception or a checked exception ? Is this something that you have to memorize???
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel Glenn wrote:
WHY is this alllowed to compile? The overriden function in class Derived is specifying an exception (NumberFormatException) that is not specified/declared by the Base class. From my understanding it is allowed to compile because NumberFormatException is a runtime exception and not a checked exception, correct?


That's exactly right.

Rachel Glenn wrote:
How do you know, in general,if "xxxException" is a runtime exception or a checked exception ? Is this something that you have to memorize???


Well, you have to memorize that unchecked exceptions are the ones that are subclasses (directly or indirectly) of Error or RuntimeException. You can then look up your exception in JavaDocs and see its class hierarchy. For certification exams, they may want you to memorize, for a small set of exception classes, which are checked or unchecked. I'm not sure what that list is, but I bet it's fairly short. NumberFormatException is probably on it though.
 
Rachel Glenn
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:
Rachel Glenn wrote:
WHY is this alllowed to compile? The overriden function in class Derived is specifying an exception (NumberFormatException) that is not specified/declared by the Base class. From my understanding it is allowed to compile because NumberFormatException is a runtime exception and not a checked exception, correct?


That's exactly right.

Rachel Glenn wrote:
How do you know, in general,if "xxxException" is a runtime exception or a checked exception ? Is this something that you have to memorize???


Well, you have to memorize that unchecked exceptions are the ones that are subclasses (directly or indirectly) of Error or RuntimeException. You can then look up your exception in JavaDocs and see its class hierarchy. For certification exams, they may want you to memorize, for a small set of exception classes, which are checked or unchecked. I'm not sure what that list is, but I bet it's fairly short. NumberFormatException is probably on it though.


ugh! thanks! I can't memorize to save my life!!! But I will do my best....

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic