• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

throw, throws

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your main method in your example would throw the Exception not to the "method" method, but back to main's caller, crashing the program.
 
reply
    Bookmark Topic Watch Topic
  • New Topic