Difference between throw and throws in Java
void doCalc() throws ArithmeticException { . . throw new ArithmeticException("sorry"); }
throw
- Java throw keyword is used to explicitly throw an exception.
- Checked exception cannot be propagated using throw only.
- Throw is followed by an instance.
- Throw is used within the method.
- You cannot throw multiple exceptions.
throws
- Java throws keyword is used to declare an exception.
- Checked exception can be propagated with throws.
- Throws is followed by class.
- Throws is used with the method signature.
- You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.