«Back to Home

Core Java

Topics

Difference Between Throw And Throws In Java

Differentiate between Throw and Throws
 
In Java, throw keyword is used to throw an exception explicitly and throws keyword is used to handle checked exception. By using throws, we can declare multiple exceptions simultaneously.
 
How can we differentiate between throw and throws keywords?

Let’s see the differences between throw and throws, given below.

 Throw  Throws
Throw keyword is mainly used to throw an exception explicitly in Java. Throws keyword is mainly used to declare an exception in Java.
Checked exception cannot be propagated by throw keyword. Checked exception can be propagated by throws keyword.
Throw keyword is followed by an object. Throws keyword is followed by the class.
Throw keyword is used within the method body. Throws keyword is used with the method declaration or the signature.
We cannot throw multiple exceptions by throw keyword. We can declare multiple exceptions by throws keyword. For example,
public void method()throws IOException,SQLException.
Throw keyword example.

void msg(){
throw new ArithmeticException(“Exception occur”);
}
Throw keyword example.

void msg() throws ArithmeticException{
// method code
}
 
Summary

Thus, we learnt that throw keyword is used to throw an exception explicitly and throws keyword is used to declare an exception and also learnt their differences in Java.