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
How do you define a parameter?
Can we overload final method in java?
How do generics work in java?
What does flagged out mean?
Explain an algorithm to find depth of a binary tree.
What is public static void main?
How is string immutable in java?
What is foreach loop in java?
Can I learn java in 3 months?
What are format specifiers in java?
What does it mean to flush a file?
What is difference between identifier and variable?
Why char array is preferred over string for storing password?
What does math floor () do?
Which are different kinds of source code?