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 .


Read more: http://javarevisited.blogspot.com/2012/02/difference-between-throw-and-throws-in.html#ixzz1xaAnuuuu

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can an integer be null java?

562


What is main string [] args?

524


What is the purpose of a transient variable?

588


Can an integer be a string?

515


What are the two environment variables that must be set in order to run any java programs?

535






What is the difference between a loader and a compiler?

530


How to add menushortcut to menu item?

567


What is class variable java?

587


What are "methods" and "fields"?

579


What are the parts of methodology?

549


What are the new features in java 8?

576


What are the differences between processes and threads?

522


Can a abstract class be declared final?

571


What is compiler and what its output.

636


How is it possible for two string objects with identical values not to be equal under the == operator?

530