The throw and throws is the concept of exception handling where the throw keyword throw the exception explicitly from a method or a block of code whereas the throws keyword is used in signature of the method.

There are many differences between throw and throws keywords. A list of differences between throw and throws are given below:

Sr. no.

Basis of Differences

throw

throws

1.

Definition

Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code.

Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.

2.

Type of exception Using throw keyword, we can only propagate unchecked exception i.e., the checked exception cannot be propagated using throw only.

Using throws keyword, we can declare both checked and unchecked exceptions. However, the throws keyword can be used to propagate checked exceptions only.

3.

Syntax

The throw keyword is followed by an instance of Exception to be thrown.

The throws keyword is followed by class names of Exceptions to be thrown.

4.

Declaration

throw is used within the method.

throws is used with the method signature.

5.

Internal implementation

We are allowed to throw only one exception at a time i.e. we cannot throw multiple exceptions.

We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException.

 

Java throw Example

TestThrow.java

  1. public class TestThrow {  
  2.     //defining a method  
  3.     public static void checkNum(int num) {  
  4.         if (num < 1) {  
  5.             throw new ArithmeticException("\nNumber is negative, cannot calculate square");  
  6.         }  
  7.         else {  
  8.             System.out.println("Square of " + num + " is " + (num*num));  
  9.         }  
  10.     }  
  11.     //main method  
  12.     public static void main(String[] args) {  
  13.             TestThrow obj = new TestThrow();  
  14.             obj.checkNum(-3);  
  15.             System.out.println("Rest of the code..");  
  16.     }  
  17. }  

Output:

Difference between throw and throws in Java

Java throws Example

TestThrows.java

  1. public class TestThrows {  
  2.     //defining a method  
  3.     public static int divideNum(int m, int n) throws ArithmeticException {  
  4.         int div = m / n;  
  5.         return div;  
  6.     }  
  7.     //main method  
  8.     public static void main(String[] args) {  
  9.         TestThrows obj = new TestThrows();  
  10.         try {  
  11.             System.out.println(obj.divideNum(450));  
  12.         }  
  13.         catch (ArithmeticException e){  
  14.             System.out.println("\nNumber cannot be divided by 0");  
  15.         }  
  16.           
  17.         System.out.println("Rest of the code..");  
  18.     }  
  19. }  

Output:

Difference between throw and throws in Java

Java throw and throws Example

TestThrowAndThrows.java

  1. public class TestThrowAndThrows  
  2. {  
  3.     // defining a user-defined method  
  4.     // which throws ArithmeticException  
  5.     static void method() throws ArithmeticException  
  6.     {  
  7.         System.out.println("Inside the method()");  
  8.         throw new ArithmeticException("throwing ArithmeticException");  
  9.     }  
  10.     //main method  
  11.     public static void main(String args[])  
  12.     {  
  13.         try  
  14.         {  
  15.             method();  
  16.         }  
  17.         catch(ArithmeticException e)  
  18.         {  
  19.             System.out.println("caught in main() method");  
  20.         }  
  21.     }  
  22. }  

Output:

Difference between throw and throws in Java