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
Greenhorn
Joined: Nov 09, 2003
Posts: 27
posted
0
Throw: 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. Throws: this can be used to inform JRE that this method may throw some type of exception. Example: public Object pop() throwsEmptyStackException { Object obj; if (size == 0) throw new EmptyStackException(); obj = objectAt(size - 1); setObjectAt(size - 1, null); size--; return obj; } Ravi
ravi<br />scjp 1.4
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
10
posted
0
throw - a command to "throw an exception" throws - a declarative statement stating that "this method throws an exception"
JavaBeginnersFaq "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
posted
0
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
posted
0
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.