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 anagram word?
Is an empty arraylist null?
How finally used under exception handling?
What is an array in java?
What is singleton class and how can we make a class singleton?
What is complexity in java?
How do you declare an empty string?
How to implement an arraylist in java?
What is length in java?
How many characters is 2 bytes?
Difference between java and javascript
What is fail fast in java?
What is runtime polymorphism or dynamic method dispatch?
Is java owned by oracle?
Explain an algorithm to find depth of a binary tree.