Skip to content
Related Articles

Related Articles

Difference between throw and throws in Java
  • Difficulty Level : Basic
  • Last Updated : 06 Feb, 2019
GeeksforGeeks - Summer Carnival Banner

Prerequisite: Throw and Throws in Java

The differences between throw and throws are:

  1. Point of usage
    • throw keyword is used inside a function. It is used when it is required to throw an Exception logically.




      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      throw new ArithmeticException();
      }
    • throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.




      void Demo()
      {
      // Statements where exceptions might occur.
      }
  2. Number of exceptions thrown
    • throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.
      For example:




      // throwing only an IOException
      throw new IOException();
    • throws keyword can be used to declare multiple exceptions, separated by comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then.
      For example:




      // throwing multiple exceptions
      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      }
  3. Syntax
    • Syntax of throw keyword includes the instance of the Exception to be thrown.
      For example:






      // throwing instance of IOException
      throw new IOException();
    • Syntax of throws keyword includes the class names of the Exceptions to be thrown.
      For example:




      // throwing multiple exceptions by class names
      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      }
  4. Propagation of Exceptions
    • throw keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using throws keyword.
      For example:




      void main() throws IOException
      {
      // Using throw for ArithmeticException
      // as it is unchecked in throws
      throw new ArithmeticException();
      }
    • throws keyword is used to propagate the checked Exceptions only.
      For example:




      void main() throws IOException
      {
      }
  5. Example:
    • throw:




      public class GFG {
        public static void main(String[] args)
        {
         // Use of unchecked Exception
         try {
          // double x=3/0;
          throw new ArithmeticException();
         }
         catch (ArithmeticException e) 
         {
           e.printStackTrace();
         }
        }
      }

      Output:

      java.lang.ArithmeticException: / by zero
          at UseOfThrow.main(UseOfThrow.java:8)
      
    • throws:




      import java.io.IOException;
        
      public class UseOfThrowAndThrows {
        
          public static void main(String[] args) 
              throws IOException
          {
          }
      }

      Output:

      Exception in thread "main" java.io.IOException
          at UseOfThrowAndThrows.main(UseOfThrow.java:7)
      
throwthrows
throw keyword is used to throw an exception explicitly.throws keyword is used to declare one or more exceptions, separated by commas.
Only single exception is thrown by using throw.Multiple exceptions can be thrown by using throws.
throw keyword is used within the method.throws keyword is used with the method signature.
Syntax wise throw keyword is followed by the instance variable.Syntax wise throws keyword is followed by exception class names.
Checked exception cannot be propagated using throw only.Unchecked exception can be propagated using throw.For the propagation checked exception must use throws keyword followed by specific exception class name.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :