File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Difference Between Throw and Throws. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference Between Throw and Throws." Watch "Difference Between Throw and Throws." New topic

Difference Between Throw and Throws.

Mahi Ranga
Ranch Hand

Joined: Jan 27, 2011
Posts: 35

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.

James Hodgkiss
Ranch Hand

Joined: Jan 22, 2004
Posts: 401
Hiya MahiRanga,

Hope that helps!

Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4629

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.


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.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

"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".

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
To the Batmobile . . . er . . . Java™ Tutorials!
Vas Miriyala
Ranch Hand

Joined: Sep 14, 2009
Posts: 114

Nice explanation guys,thanks for all your answers.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
You're welcome
I agree. Here's the link:
subject: Difference Between Throw and Throws.
jQuery in Action, 3rd edition