• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does a method/class necessarily need to have to throw an exception if it declares so?  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This question may seem too simple for you.. please help me understand it ..

Does my method/class that declares to throw an exception need to throw it?
i mean what if my class/method has declared that it will throw an exception and does not have a throw statement in its body?

thanks in advance..
 
Java Cowboy
Sheriff
Posts: 16083
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

To answer your question: no, it doesn't.

Why don't you try it out? Write a small test program with a method that declares to throw an exception but doesn't actually throw it, and try to compile it. Experimenting with Java yourself is the best way to learn it.
 
Marshal
Posts: 58462
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find the 4th edition of Thinking in Java™ by Bruce Eckel, it explains why that feature can be useful.
 
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the actual answer is somewhat more complex than "no, it doesn't." When writing the small test program Jesper suggests, try it with both a checked exception (like, say, IOException) and an unchecked exception (like NullPointerException).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!