throws used to declare an exception, and the throw keyword is used to explicitly throw an exception.
If we see the wise syntax, then throw followed by an instance variable, and throws followed by the names of the exception classes.
The throw keyword is used inside the method body to throw exception, and the throws used in the method declaration (signature).
for instance
throw
throw new Exception("You have some exception") throw new IOException("Connection failed!!")
throws
public int myMethod() throws IOException, ArithmeticException, NullPointerException {}
You cannot throw multiple exceptions with throw . You can declare a few exceptions, for example. public void method () throws an IOException, SQLException.
checked exceptions cannot be propagated using throw just because it is explicitly used to throw a specific exception. checked exception can be thrown using throws .
Propagation of exceptions: An exception propagates from method to method, to the call stack, until it is caught. Therefore, if a () calls b (), which calls c (), which calls d (), and if d () throws an exception, the exception will propagate from d to c to b to a, unless only one of these methods will catch the exception. What is exception propagation?
source share