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
Can an integer be null java?
What is main string [] args?
What is the purpose of a transient variable?
Can an integer be a string?
What are the two environment variables that must be set in order to run any java programs?
What is the difference between a loader and a compiler?
How to add menushortcut to menu item?
What is class variable java?
What are "methods" and "fields"?
What are the parts of methodology?
What are the new features in java 8?
What are the differences between processes and threads?
Can a abstract class be declared final?
What is compiler and what its output.
How is it possible for two string objects with identical values not to be equal under the == operator?