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

1 Answer

0 votes
by

The difference between throw and throws in Java?

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

+3 votes
asked May 13, 2021 in JAVA by rajeshsharma
+1 vote
asked May 30, 2020 in JAVA by Robindeniel
...