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”

  1. throw new Exception("Exception message")
throw new Exception("Exception message")


“throws”

  1. 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 :

  1. public int divide(int x,int y) throws ArithmeticException, NullPointerException {
  2.  
  3.       //throwing arithmetic exception based on some condition using throw
  4.       if(y==0){
  5.       throw new ArithmeticException("Can not perform division as denominator is zero!!");
  6.    }
  7.  
  8. }
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

  1. public class ThrowThrowsExample{  
  2.   public double divide(int num1, int num2) throws ArithmeticException{
  3. double res=0.0;
  4. if(num2 == 0 ) {
  5.       throw new ArithmeticException("Denominator cannot be 0");
  6. }
  7. else{
  8.       res=num1/num2;
  9. }
  10. return res;  
  11. }
  12.    public static void main(String args[]){  
  13.     ThrowThrowsExample obj = new ThrowThrowsExample();
  14.     try{
  15.        System.out.println(obj.divide(10,0));  
  16.     }
  17.     catch(ArithmeticException e){
  18.        System.out.println(e.getMessage());
  19.     }
  20.    }  
  21. }
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());
	}
   }  
}


About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on