what is the difference between throw and throws clause in java????
mmm.....i no that all methods in java use the throw statement to throw an exception and the throw statement requires a single argument : a throwable object....
but plz can any1 tell me the function of the throws clause?????

7 Years
Discussion Span
Last Post by shakeelahmed22

throws appears as part of the declaration of a method. It tells you that when you call that method the named Exception may be thrown by that method, and that you need to handle it in some way.


The answer is simple English grammar:

A method throws an Exception:

public void method(int i) throws Exception {


The method throws an Exception when you call it

But you throw an Exception:

if (i<0) throw new Exception();

If 'i' is negative you will throw an Exception.

And if you put it together:

public void method(int i) throws Exception {
      if (i<0) throw new Exception();
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.