Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's happens when I omit the throws keyword?  RSS feed

 
Christian Wansart
Ranch Hand
Posts: 32
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

sorry for this dumb question, but I think I misunderstood something with exceptions. I thought I have to add the throws <Exceptionname> to my method signature when I want to "pass it" to the caller. However, the following code works in my Eclipse:


What did I miss here?
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The throws keyword is used to declare checked exceptions. IllegalArgumentException is an unchecked exception, so declaring it with the throws keyword is unnecessary.

Checked exceptions are all sub-types of Exception that are not sub-types of RuntimeException.

That means that you don't have to declare Errors and RuntimeExceptions.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:. . . Checked exceptions are all sub-types of Exception that are not sub-types of RuntimeException. . . .
… plus Throwable; it is all in the Java┬« Language Specification (but that can be difficult to read)
 
Christian Wansart
Ranch Hand
Posts: 32
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, *that* I did not know of. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!