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


Please Help Members By Posting Answers For Below Questions

How many types of methods are there in java?

530


Is linkedlist thread safe in java?

565


What is jit and its use?

589


why java does not support unsigned keyword?

4271


How do you write a scanner class in java?

570






5 Coding best practices you learned in java?

640


What is Java Annotations?

559


What are the topics in advance java?

525


Explain the JDB in depth & command line.

575


Why vector is used in java?

566


What is the syntax and characteristics of a lambda expression?

563


Can we overload final method in java?

557


Why pass by reference is not possible in java?

502


Is singleton class thread safe?

542


What are voids?

539