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).]