Can someone tell me the difference between throw and throws keyword alongwith appropriate examples.
Dóra Takács wrote:The throws keyword is used in method declarations...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Dóra Takács wrote:
Can someone tell me the difference between throw and throws keyword alongwith appropriate examples.
2.)----------------------------------------------------------------------------------------------
The throw keyword:
Here, the throw says that something actually went wrong. You know for sure, that something happened that should not have happened, and you anticipated the possibility. In this case, without exception handling, the execution of the method would end here, and unless the programmer invoking your method handled the possible exception, the program will end with a stack trace of the exception, so you would see what happened, what went wrong.
Summary) -------------------------------------------------------------------------------------
The point is, with throws, you declare that something might go wrong in your method.
With throw, you define what kind of problem occoured, and you know for sure that something did indeed, go wrong during execution.
Also, as you see in the example, the throw keyword requires you to have an exception object, which you might aswell create on the same line.
Best regards, Dóri
I am afraid those answers you made are not clear. I suspect they are incorrect, but I am not sure.khadar valli wrote:. . .
1.Throw clause in used to declare an exception and throws keyword is used to throw an exception explicitly to its Calling method or Super Class.
. . .
4. To throw an exception we use throw keyword while to handle (catch) an exception we use throws clause.
. . .
I still dont understand that why do you need to unnecessarily throw an exception using "throw"
I particularly like your inventive use of colour too - although I wouldn't go too overboard with it...
Consider Paul's rocket mass heater. |