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
How many types of methods are there in java?
Is linkedlist thread safe in java?
What is jit and its use?
why java does not support unsigned keyword?
How do you write a scanner class in java?
5 Coding best practices you learned in java?
What is Java Annotations?
What are the topics in advance java?
Explain the JDB in depth & command line.
Why vector is used in java?
What is the syntax and characteristics of a lambda expression?
Can we overload final method in java?
Why pass by reference is not possible in java?
Is singleton class thread safe?
What are voids?