Difference between throw and throws in Java

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.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      throw new ArithmeticException();
      }

      chevron_right

      
      

    • throws keyword is in the function signature. It is used when the function has some statements that can lead to some exceptions.
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      void Demo()
      {
      // Statements where exceptions might occur.
      }

      chevron_right

      
      

  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:



      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // throwing only an IOException
      throw new IOException();

      chevron_right

      
      

    • 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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // throwing multiple exceptions
      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      }

      chevron_right

      
      

  3. Syntax
    • Syntax of throw keyword includes the instance of the Exception to be thrown.
      For example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // throwing instance of IOException
      throw new IOException();

      chevron_right

      
      

    • Syntax of throws keyword includes the class names of the Exceptions to be thrown.
      For example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      // throwing multiple exceptions by class names
      void Demo() throws ArithmeticException, NullPointerException
      {
      // Statements where exceptions might occur.
      }

      chevron_right

      
      

  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:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      void main() throws IOException
      {
      // Using throw for ArithmeticException
      // as it is unchecked in throws
      throw new ArithmeticException();
      }

      chevron_right

      
      

    • throws keyword is used to propagate the checked Exceptions only.
      For example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      void main() throws IOException
      {
      }

      chevron_right

      
      

  5. Example:
    • throw:
      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      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();
         }
        }
      }

      chevron_right

      
      

      Output:

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

      edit
      close

      play_arrow

      link
      brightness_4
      code

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

      chevron_right

      
      

      Output:

      Exception in thread "main" java.io.IOException
          at UseOfThrowAndThrows.main(UseOfThrow.java:7)
      
throw throws
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.


My Personal Notes arrow_drop_up

Geeksforgeeks Intern

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.