wood burning stoves*
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
Author

throw, throws

Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Hi all-
I'm having trouble understanding the throw/throws statement.
More so on the throws.
Can anyone out there explain the process of how the throws statement works? It would be Greatly appreciated!
Thanks
Jimmy-
-----------------------
yah, yah I'm a newbie
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
While declaring a method and if it may throw one or the other exception you use in the method header throws (because there may be several ==> the s after throw) which (different) exception(s) may be thrown.
And you use throw if you discovered yourself something wrong and want to follow an exceptional path through your application.
Here an example of a 'mathbeans' package:
<pre>
synchronized public Expression parse(String string) throws ParseError {
// Defines an expression from a string. (The expression must
// not be boolean-valued).
if (string == null | | string.trim().length() == 0)
throw new ParseError("No input provided in expression definition!",0,data);
pos = 0;
data = string;
ttype = NONE;
Expression ans = parseLogicalExpression(); // ans might or might not be boolean-valued
if (peek() != EOS)
throw new ParseError("Extra data found after the end of a complete legal expression.",pos,data);
if (ans.isBooleanValued())
throw new ParseError("Logical, boolean-valued expression found where only an ordinary numerical expression is legal.",pos,data);
return ans;
}

</pre>
[This message has been edited by Peter Gragert (edited June 20, 2001).]
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
All Java methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. In the Java system, throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement:
throw someThrowableObject;
If you attempt to throw an object that is not throwable, the compiler refuses to compile your program and displays an error message similar to the following:
testing.java:10: Cannot throw class java.lang.Integer;
it must be a subclass of class java.lang.Throwable.
throw new Integer(4);
public Object pop() throws EmptyStackException
{
Object obj;
if (size == 0) throw new EmptyStackException();
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
size--;
return obj;
}
You'll notice that the declaration of the pop method contains this clause:
throws EmptyStackException
The throws clause specifies that the method can throw an exception i.e. EmptyStackException in this example.

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Bob Graffagnino
Ranch Hand

Joined: May 30, 2001
Posts: 81
Let's say a method you've written generates an exception. There are two ways to deal with it.
1. Use a try-catch block to catch the exception and deal with it.
2. Declare your method with a "throws" clause.
Example: public void myMethod() throws SomeException{/*your code*/}
Using the throws keyword tells the compiler, "if an exception occurs, I don't want to deal with it. Please pass it back to whoever called me."
Using a try-catch block tells the compiler, "if an exception occurs, I'll deal with it."
Of course, a catch block can (if you want to) throw its own exception, which will be passed back to whoever called your method. In this case, you would need a throws clause in your method declaration.
Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
OK-
So 2 methods can throw exceptions back at each other?
void method1() throws Excepton
{
throw new Exception();
}
public static void main(String [] args)throws Exception
{
class1 x = new class1();
x.method1();
}
method1 defined in above throws Exception to the caller,
which in this case is the main method. Which in turn throws it back. Is this correct?
Thanks
Jimmy-
------------------
yah, yah I'm a newbie
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Your main method in your example would throw the Exception not to the "method" method, but back to main's caller, crashing the program.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: throw, throws
 
Similar Threads
== and .equals()
exception handling
help
Interfaces (basically speaking)
try block