Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between throw and throws keywords  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner in Java..
Curious to know about the difference between thow and thows in exception
 
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Kelly :

The throws ( with an s at the end ) keyword is used to specify that if a method is is going to throw any exception at all. On the other hand , the throw keyword is used to explicitly throw an exception. For example


A very simple question arises that what is the purpose of declaring a method with throws, when you are not throwing any exception. The answer is that sometimes you want the Exception to propagate to the method stack above. The two keywords can be used in conjunction like this



Do post back for any further queries.
 
Marshal
Posts: 58382
178
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I suggest you find a tutorial about Java exceptions: there is a good one here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!