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?????

Recommended Answers

All 4 Replies

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();
}

hey cherill .....thanx a heap
i have taken that down.....

hello mr javaaddict...thanx a heap for ur reply...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.