throw vs throws in java
Before going through this article, Please understand basics of throw and throws from my previous articles “throw” and “throws”.
Let’s understand the difference between throw and throws
"throw" keyword is used to throw an exception explicitly "throws" clause is used to declare an exception
It means whenever we want to throw an exception in the code, we should use throw keyword and throws should be used to declare an exception instead of handling it, so throws acts as an alternative to try/catch
"throw" is followed by an instance of Exception class "throws" is followed by exception class names
In other words,
when we use throw, we must create an object of exception which we are going to throw
When we use throws , we must specify exception class name which we don’t want to handle
Example :
“throw”
- throw new Exception("Exception message")
throw new Exception("Exception message")
“throws”
- public int divide(int x,int y) throws ArithmeticException, NullPointerException {}
public int divide(int x,int y) throws ArithmeticException, NullPointerException {}
"throw" is used inside method body to throw an exception explicitly based on some condition "throws" clause is used in method declaration (signature).
Example :
- public int divide(int x,int y) throws ArithmeticException, NullPointerException {
- //throwing arithmetic exception based on some condition using throw
- if(y==0){
- throw new ArithmeticException("Can not perform division as denominator is zero!!");
- }
- }
public int divide(int x,int y) throws ArithmeticException, NullPointerException { //throwing arithmetic exception based on some condition using throw if(y==0){ throw new ArithmeticException("Can not perform division as denominator is zero!!"); } }
We can "throw" only one exception at a time But, We can handle multiple exceptions by declaring them using "throws" keyword
Note :
Whenever we need to stop further execution of code based on some business logic, we must throw an exception there itself. We need to handle thrown exception there itself using try/catch or sometime it makes sense to use throws to avoid handling that exception in the same place and provide the responsibility of handling that exception to caller.
Complete example using throw and throws together
- public class ThrowThrowsExample{
- public double divide(int num1, int num2) throws ArithmeticException{
- double res=0.0;
- if(num2 == 0 ) {
- throw new ArithmeticException("Denominator cannot be 0");
- }
- else{
- res=num1/num2;
- }
- return res;
- }
- public static void main(String args[]){
- ThrowThrowsExample obj = new ThrowThrowsExample();
- try{
- System.out.println(obj.divide(10,0));
- }
- catch(ArithmeticException e){
- System.out.println(e.getMessage());
- }
- }
- }
public class ThrowThrowsExample{ public double divide(int num1, int num2) throws ArithmeticException{ double res=0.0; if(num2 == 0 ) { throw new ArithmeticException("Denominator cannot be 0"); } else{ res=num1/num2; } return res; } public static void main(String args[]){ ThrowThrowsExample obj = new ThrowThrowsExample(); try{ System.out.println(obj.divide(10,0)); } catch(ArithmeticException e){ System.out.println(e.getMessage()); } } }