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 bytes is a string java?
What is the use of math abs in java?
What are the default and parameterized constructors?
Why scanner is used in java?
What is class variable java?
How to use Media tracker Class.
What is string literal in java?
What are the files generated after using IDL to java compiler?
What is entry in java?
What are predicates in java 8?
What are measurable parameters?
What is collection sort in java?
What are internal and external variables?
What is the program development process?
What is exception hierarchy in java?