This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference Between Throw and Throws.

 
Mahi Ranga
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what is the difference between throw and throws? and when can i use throw and when can i use throws.
Please tell me.
Advanced thanks for your Answer.

Regards,
MahiRanga.
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya MahiRanga,



Hope that helps!

Cheers,
James
 
Stephan van Hulst
Bartender
Pie
Posts: 5588
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use throws in a method signature to indicate that a method is capable of throwing a particular exception. Any code calling this method will have to deal with the possibility that that particular exception can occur.

The throw keyword is used to actually throw an exception. It can be used wherever you can place a statement.

[eta]

In James' above example, any code that calls checkString() will have to deal with the fact that it *can* throw an exception, and checkString() actually throws an exception if the argument is null.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"throw" is a command. "Mahi, throw the ball". I am telling you to do something.

"throws" is used to describe something. "Mahi throws a curveball, a fastball, and a slider". I'm telling someone what you can do (or will potentially do in the future).

In java, 'throws' is used to tell the compiler what kind of exceptions a method might pass back up to the caller.

"throw" is used to tell the JVM "hey, you need to send this up to the caller NOW".
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Batmobile . . . er . . . Java™ Tutorials!
 
Vas Miriyala
Ranch Hand
Posts: 114
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation guys,thanks for all your answers.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic