aspose file tools*
The moose likes Beginning Java and the fly likes throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws" Watch "throws" New topic
Author

throws

geetanjali rajput
Greenhorn

Joined: Apr 02, 2002
Posts: 19
i referred two books which use throw stmt while defining throws. does this mean we always use throw when we need to use throws stmt. . please help . i really need to know it's answer.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by geetanjali rajput:
i referred two books which use throw stmt while defining throws. does this mean we always use throw when we need to use throws stmt. . please help . i really need to know it's answer.

Hmmm...I'm confused. I'm not really sure what you mean. Try to be a little clearer.
Quickly, though, you're always required to either catch all checked expections or declare that the method your in is capable of throwing them. If you don't take one of these two actions, the compiler will give you an error.
If you can clear up your question a bit, perhaps I can give you more information.
Regardless, I hope this helps, at least a little.
Corey


SCJP Tipline, etc.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
I believe the methos is declaring that it throws a specific exception in it's declaration. Then in it's body it actualy throws the exception. i.e It is defering handling the exception up the call stack.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
There are two kinds of exceptions: checked and unchecked (or sometimes runtime).
Okay, suppose that a method declares that it throws a checked exception. You can imagine this method as some sort of unstable firecracker just waiting to blow. Let's call it method A.
A second method that calls this method (let's call it method B) has two options:

(1) deal with the problem within B's scope with a try-catch block (in which case a throws clause is now unnecessary but still possible), or
(2) shrug its shoulders and say, "to heck with it, let some other fella deal with this," and then method B must declare a throws clause, (throwing exactly the same exception), warning all and sundry of the peril of calling this method.
The guy who implements method B doesn't need to declare a throw statement. If method A throws an exception, it will propagate to the calling method (method B) in which case it is as if method B threw the exception.
However, method B can choose to throw the same exception as well, if it decides that certain conditions are propitious. In which case it will use the throw statement. If it does decide to use this option, then it must declare that it is throwing this exception in its throws clause.
To sum up:
if you see a method with a throws clause, then either:
(1) it uses a throw statement somewhere within its implementation, and/or
(2) it is calling a method that throws the same exception too.
-anthony
[ April 02, 2002: Message edited by: Anthony Villanueva ]
geetanjali rajput
Greenhorn

Joined: Apr 02, 2002
Posts: 19
thanks all of you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: throws