Difference between throw and throws? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Difference between throw and throws?

25th Mar 2018, 6:25 PM
Ruchit Porwal
Ruchit Porwal - avatar
2 Answers
+ 5
throws clause is used to declare an exception and throw keyword is used to throw an exception explicitly. ... The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature)
25th Mar 2018, 6:44 PM
Baraa AB
Baraa AB - avatar
+ 2
In Java the difference between throw and throws is: Throw is used to create an exception which the programmer has to deal with (or not) like... throw new EmptyStackException(); Throws on the other hand is used to tell the compiler that some exception(s) may occur like... public void myMethod() throws EmptyStackException { // code throw new EmptyStackException(); } Adding ‘throws’ is mandatory if the exception is checked meaning it extends from the Exception class (EmptyStackException is not, so you don’t need the throws syntax)
25th Mar 2018, 6:30 PM
TurtleShell
TurtleShell - avatar