+ 2
Difference between throw and throws?
2 odpowiedzi
+ 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)
+ 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)