• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between throw and throws keywords  RSS feed

 
KellyDeek Jones
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
 
Saif Asif
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.
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • 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!