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 .
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
What is the final method?
Why is static used?
How do you sort arrays in java?
What is a pattern what is an anti pattern?
What is the flag in java?
What is the purpose of a default constructor?
Explain the difference between throw and throws in java?
What are the main features of java?
Is vector ordered in java?
What is string data type?
What is n in java?
Can we override constructor?
Is alive method in java?
What are java packages? What is the significance of packages?
What are some examples of variable costs?