Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Java
  • 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?
 
Saloon Keeper
Posts: 8754
162
  • 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.
 
Marshal
Posts: 58830
179
  • 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: 37
Firefox Browser IntelliJ IDE Java
  • 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!