Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes throw throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throw throws" Watch "throw throws" New topic

throw throws

feroz muhamed
Ranch Hand

Joined: Nov 15, 2003
Posts: 87
what is the difference btwn throw and throws?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
throw is used normally like this
throw new Exception(). This throws a new Exception wich must be catched or passed further to the calling class or method.
throws is used in the method signature. This tells the compiler that this method doesn't take care of the given exception but simply passes it to the caller. So every class/method which calls that method must either catch this exception or itself throws it.
Ravi Kotaru

Joined: Nov 09, 2003
Posts: 27
Before you can catch an exception, some Java code somewhere must throw one. Any Java code can throw an exception: your code, code from a package written by someone else (such as the packages that come with the Java development environment), or the Java runtime system. Regardless of who (or what) throws the exception, it's always thrown with the Java throw statement.
this can be used to inform JRE that this method may throw some type of exception.
public Object pop() throws EmptyStackException {
Object obj;
if (size == 0)
throw new EmptyStackException();
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
return obj;

ravi<br />scjp 1.4
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
throw - a command to "throw an exception"
throws - a declarative statement stating that "this method throws an exception"

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Leslie Chaim
Ranch Hand

Joined: May 22, 2002
Posts: 336
throw - is a verb
throws - is an action verb

Normal is in the eye of the beholder
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
If I may throw in my 2 cents here ...
You may only "throw" objects, whereas "throws" is a declaration, and thus can only be defined by a class.
That is, you declare a function to throw a type (class) of object with the "throws" keyword, but when the time comes to actually throw an object of that class, you do so with the "throw" keyword.
public void throwMe() throws Exception /* <-- that's a class */ {
throw new Exception(); // <--- that's an object
Wow, I think that will even compile.

I agree. Here's the link:
subject: throw throws
It's not a secret anymore!