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
Why is a constant variable important?
What is the difference between instanceof and isinstance?
Explain a few methods of overloading best practices in java?
Do you know how to reverse string in java?
Explain list interface?
What is the difference between char and char *?
Can a class be private?
Explain different ways of creating a thread. Which one would you prefer and why?
Is a class subclass of itself?
What makes a function well defined?
Why multiple inheritance is not supported by java?
explain local datetime api in java8?
Which language is java?
Can we override final method?
What is meant by inheritance and what are its advantages?