5 Difference Between throw and throws in Java with Examples

The definition and usage of throw and throws in java are the basis of understanding exception handling in java and are commonly asked questions in any java interviews. In this article, I will be sharing the definition of throw and throws keyword, the difference between throw and throws in java and examples.

Read Also : Difference between Checked and Unchecked Exception in Java

Difference between throw and throws in Java

1. Definition: throw is a statement and used in a method to explicitly throw an exception.
throws keyword is used by a method to specify which exceptions can be thrown from the method.


2. Place of declaration: First we need to understand what is method signature. Method signature is a part of method declaration. Method signature is a combination of method name and parameters list.

throw is used with in method. throws is used with the method signature.

/* throws keyword is used with the 
   method signature(combination of 
   method name and parameters list) 
   as shown below */ 
public void JavaHungry(String str)throws ArithmeticException, NullPointerException
{
    /* throw keyword is used inside method

       named JavaHungry */ 
 
       throw new ArithmeticException(); 
} 


3. Syntax: throw is followed by an object(instance) of Exception class while throws is followed by Exception class names.

For example,
/* throws keyword is followed by

   ArithmeticException and 

   NullPointerException, both of them

   are Exception class names*/

public void JavaHungry(String str)throws ArithmeticException, NullPointerException

{
      /* throw keyword is followed by 

         object(instance) of ArithmeticException

         as there is new keyword before the 

         ArithmeticException*/

         throw new ArithmeticException(); 
} 

4. Number of exceptions thrown: throw statement can only throw one exception at a time.
throws can be used to throw multiple exceptions, separated by comma ( , ).
For example,

/* throws keyword is followed by

   multiple exceptions i.e.

   ArithmeticException and 

   NullPointerException, both of them

   are separated by comma*/

public void JavaHungry(String str)throws ArithmeticException, NullPointerException
{
      /* throw keyword is followed by only 

         one exception i.e ArithmeticException */

         throw new ArithmeticException(); 
} 


5. Examples:

Throw statement example given below:

public class ThrowExample { 
 
  public static void checkPassEligibility(float totalMarks){
    if(totalMarks > 33){
        System.out.println("Student passed");
    }   
    else {
        throw new ArithmeticException("Student failed");
    }
  }
    
  public static void main(String[] args){
    ThrowExample te = new ThrowExample();
    System.out.print("Hi! Please check your score");
    te.checkPassEligibility(31);
    System.out.println("Thank you!");
  }
}

Output :
Hi! Please check your score
Exception in thread "main" java.lang.ArithmeticException: Student failed
    at ThrowExample.checkPassEligibility(ThrowExample.java:7)
    at ThrowExample.main(ThrowExample.java:13)

Throws keyword example given below :

public class ThrowsExample { 
    
  public int division(int numerator,int denominator) throws ArithmeticException{
      int result = numerator/denominator;
      return result;
  }

  public static void main(String[] args){
    ThrowsExample te=new ThrowsExample();
    System.out.print("Division of numerator and denominator : ");
    te.division(50,0);
  }
}

Output :
Division of numerator and denominator :
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at ThrowsExample.division(ThrowsExample.java:4)
    at ThrowsExample.main(ThrowsExample.java:11)

Similarities between throw and throws in Java

1. Both throw and throws need to be handled and cannot be left uncaught.

Recap : Difference between throw and throws in Java



ThrowThrows
Definitionthrow is a statement and it is used to explicitly throw an exception.throws keyword is used to specify which exceptions can be thrown from the method.
Place of  declarationthrow is used inside method.

It is used with method signature.
SyntaxIt is followed by an object of the Exception classIt is followed by Exception class names.
Number of Exceptions thrownIt can only throw one exception at a time.It can throw multiple exceptions separated by a comma.

That's all for the day, please mention in comments if you have any questions or doubts related to the article, the difference between throw and throws in java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry