0 votes
in JAVA by
Explain difference between throw and throws in Java?

1 Answer

0 votes
by

the throw is used to actually throw an instance of java.lang.Throwable class, which means you can throw both Error and Exception using throw keyword e.g.

throw new IllegalArgumentException("size must be multiple of 2")

On the other hand, throws is used as part of method declaration and signals which kind of exceptions are thrown by this method so that its caller can handle them. It's mandatory to declare any unhandled checked exception in throws clause in Java. Like the previous question, this is another frequently asked Java interview question from errors and exception topic but too easy to answer.

Related questions

0 votes
asked Jan 27, 2020 in JAVA by rahuljain1
0 votes
asked Oct 17, 2020 in JAVA by rahuljain1
...