There is a difference in using these two. Assuming the second method is without s. The first approach, throws
, used in the method signature, denoting which Exception can possible be thrown by this method. You can declare multiple exception classes. For checked Exception, the caller can either handle this exception by catching it or can rethrow it by declaring another throws in method declaration.
public void f()throws IOException, SQLException {
//some logic
}
The second approach, using throw
without s is used in any part of code followed by an instance whenever you need to throw a specific exception to the caller.
public void f(){
//some logic
throw new ArithmeticException("sorry");
}
You can get more information throw and throws in Java