• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method's can't throw unchecked exceptions?  RSS feed

 
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A runtime exception may not be a part of the method signature, even if a method may throw it" - Page 372 of Mala Gupta's book (Manning). But in my test code, the line:


Doesn't prevent compilation. However, I'm using Java 6 and the book is for Java 7 (having issues installing JDK 7 on Mac). Has Java 7 changed this behaviour or is the book incorrect?
 
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This statement from the book is just a bit misleading. It's allowed to use runtime exceptions in a throws clause but this is optional. Because callers of a method aren't forced to handle runtime exceptions even if they are declared in a throws clause this is only for documentation purposes or to give other developers a hint that your method throws a runtime exception. So in effect a throws clause with only runtime exceptions is not an essential part of a method signature because it can simply be ignored but that doesn't mean that it's not allowed to put runtime exceptions in a method signature.

Marco
 
Iarla O'Riada
Ranch Hand
Posts: 87
Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marco, that resolves it for me and I can move on with my studies. Many thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!