Question:
What is the difference between throw and throws?
Answer:
Throw:it is used to raise exception explicitly that means it is use when a user defined exception is raised.
Throws:if a method is capable of throwing an exception but it does not handle the exception that must be specified by using "throws" class. Source: CoolInterview.com
they are not same.
"throws" declares that your method is capable of throwing an exception.
"throw" actually does the work of throwing the exception.
example :
public void doSomething() throws ApplicationException { try{ } catch(Exception e){ // catch all excpetions n provide some meaningful message throw ApplicationException("An error occurred while trying connect to DB"); } } Source: CoolInterview.com
Answered by: Naveen | Date: 9/10/2009
| Contact Naveen
Throw:it is used to raise exception explicitly that means it is use when a user defined exception is raised.
Throws:if a method is capable of throwing an exception but it does not handle the exception that must be specified by using "throws" class.
void Show()throws NullPointerException { throw new NullPointerException(); } Source: CoolInterview.com
Answered by: Jitendra Yadav | Date: 6/17/2010
| Contact Jitendra Yadav
Throw:it is used to raise exception explicitly that means it is use when a user defined exception is raised.
Throws:if a method is capable of throwing an exception but it does not handle the exception that must be specified by using "throws" class.
Source: CoolInterview.com
Answered by: sumathi | Date: 7/8/2010
| Contact sumathi
throw: it is used to generate an exception. throws: it is used to forward an exception. Source: CoolInterview.com
Answered by: rajesh patel | Date: 7/11/2010
| Contact rajesh patel
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|