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