What is the difference between throw and throws?
Answer Posted / rakesh nath
Throw clause is used in any part of the code where you want
to throw any specific exception to the calling method while
throws can be termed as a substitute for try-catch block.
When an exception occurs in a program, the compiler needs to
know what should be done with that exception. For that
either we should have a try-catch block or the method should
have the throws clause attached, as follows.
<access-modifier> <return-type> myMethod(<parameter-list>)
throws <exception-list>
for example, public void meth1() throws MyException
throw can handle only ONE throwable object while throws can
handle multiple throwable objects seperated by commas.
| Is This Answer Correct ? | 13 Yes | 1 No |
Post New Answer View All Answers
Explain spliterator in java8?
Is void a data type in java?
What are the benefits of immutable objects?
What is the numeric promotion?
What is unicode datatype?
What is the purpose of the finalize() method?
What is a generic data type?
What does you mean in math?
What is finalize() function in java?
Can extern variables be initialized?
Why do we use threads in java?
Objects or references which of them gets garbage collected?
Can a class with private constructor be extended?
Why is static used?
What is variable and its types?