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
Can list be null in java?
How many ways can an argument be passed to a subroutine and explain them?
why java uses class level type casting ?
Explain reverse a linked list iterative solution in java?
what is meant by Garbage collection?
What does the @override annotation do?
What is singleton class example?
What is role of void keyword in declaring functions?
What is finally block?
What is java virtual machine? Explain
Does java support Operator Overloading?
Why do we use predicate in java?
What is a subsequence of a string?
Explain the difference between association, aggregation and inheritance relationships.
How many types of array are there?