what is difference between throw and throws in exception?
Answer Posted / basant
) throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method
signature a method suggesting its caller to prepare for Exception declared in throws clause, specially in case of checked Exception and provide sufficient handling of them. On the other hand throw keyword transfer control of execution to caller by throwing an instance of Exception. throw keyword can also be used in place of return as shown in below example:
private static boolean shutdown() {
throw new UnsupportedOperationException("Not yet implemented");
}
as in below method shutdown should return boolean but having throw in place compiler understand that this method will always throw exception .
Read more: http://javarevisited.blogspot.com/2012/02/difference-between-throw-and-throws-in.html#ixzz1xaAnuuuu
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
Does printwriter create a file?
What is final variable?
Explain about varargs in java?
Can we split string with in java?
Explain about strings in java?
Java openings 3 - 5 years, Lnt Infotech. requirements - core java, J2ee, struts, hibernate Interview Date:- 19 March 2011 Time:- 9:00 AM to 12:00 Pm Interview Location - L & T Infotech, Manapakkam, Chennai Refererral PS NO:- 291649 (Please mention this when u fill the form only then u will be considered for interview) Documents Required:- Latest Resume, Photograph and last 3 payslips Mail me on vasan2211@gmail.com once u appear for interview
Can we assign the reference to this variable?
How would you format a date in java? I.e. In the ddmmyyy format?
Why do we override tostring method in java?
Given a singly linked list, determine whether it contains a loop or not without using temporary space?
what is the purpose of "virtual"?
What are the advantages of packages in java?
How do you escape a string?
Which variables are stored in heap?
What is difference between static class and singleton pattern?