Difference between “throw” and “throws”

We will discuss about uses of the keywords “throw” and “throws” with respect to exception handling.

 

The “throw” statement

A Java program can explicitly throw an exception using the throw statement besides the implicit exception thrown by the Java runtime system. The general format of the throw statement is as follows:

throw <exception reference>; 

The Exception reference must be of type Throwable class or one of its subclasses. A detail message (optional) can be passed to the constructor when the exception object is created. Examples are shown below:

throw new ArithmeticException(); //using constructor: Throwable()
                   OR
throw new NullPointerException("explicit"); //using constructor: Throwable(String message) 

 

See the coding example (Except06.java) below:

public class Except06 {
    public static void main(String[] args) {
        //calling method1()
        try {
            method1();
        } 
        catch (ArithmeticException e) {
            System.out.println("Exception1 = " + e);
        }
        //calling method2()
        try {
            method2();
        } 
        catch (NullPointerException e) {
            System.out.println("Exception2 = " + e);
        }
    }

    static void method1() {
        throw new ArithmeticException(); //explicitly thrown without a message
    }

    static void method2() {
        throw new NullPointerException("explicit"); //explicitly thrown with a detail message
    }
}

The output of the code is given below:

Exception1 = java.lang.ArithmeticException
Exception2 = java.lang.NullPointerException: explicit

 

The “throws” clause

If a method generates exceptions that it can not handle, it is the duty of the method to declare it so that callers of the method can guard themselves against those exceptions. In order to solve this problem the method must include throws clause in its declaration part. This is the general form of a method declaration that includes a throws clause:

type method (parameter-list) throws , , ... ,   {
   // body of method
}

Each can be a checked or unchecked or sometimes even a custom Exception. This is necessary for all exceptions, except those of type RuntimeException or Error, or any of their subclasses. All other exceptions that a method can throw must be declared in the throws clause. If they are not, a compile-time error will result. An example is shown below:

static void method() throws IOException, InterruptedException, ClassNotFoundException { 
   //method body
}

The exception type specified in the throws clause in the method prototype can be a super class type of the actual exceptions thrown. Also an overriding method cannot allow more checked exceptions in its throws clause than the inherited method does. The compiler enforces that the checked exceptions thrown by a method are limited to those specified in its throws clause. Of course, the method can throw exceptions that are subclasses of the checked exceptions in the throws clause.

Any method that can cause a checked exception to be thrown, either directly by using the throw statement or indirectly by invoking other methods that can throw such an exception, must deal with the exception in one of three ways. They are given below:

  • use a try block and catch the exception in a handler and deal with it.
  • use a try block and catch the exception in a handler, but throw another exception that is either unchecked or declared in its throws clause
  • explicitly allow propagation of the exception to its caller by declaring it in the throws clause of its method prototype.

 

See the coding example (Except07.java) below:

import java.io.IOException;

public class Except07 {

   public static void main(String[] args) {
      try {
         method();
      } 
      catch (IOException e) {
         System.out.println("Exception = " + e);
      }
      catch (InterruptedException e) {
         System.out.println("Exception = " + e);
      }
      catch (ClassNotFoundException e) {
         System.out.println("Exception = " + e);
      }
   }

   static void method() throws IOException, InterruptedException, ClassNotFoundException {
      double n = Math.random();

      if (n < 0.3) {
        throw new IOException("explicit 1"); 
      } 
      else if (n < 0.6) {
        throw new InterruptedException("explicit 2"); 
      } 
      else {
        throw new ClassNotFoundException("explicit 3"); 
      }
   }
}

 

​The output of the code may exhibit one of the three possibilities as given below:

Exception = java.io.IOException: explicit 1         
                          OR
Exception = java.lang.InterruptedException: explicit 2
                          OR
Exception = java.lang.ClassNotFoundException: explicit 3

 

The differences between throw and throws are given below:~

Sl. No.Java throw keywordJava throws keyword
1The “throw” keyword is used to explicitly throw an exception.The “throws” keyword is used to declare an exception.
2This keyword is followed by a Throwable instance.This keyword is followed by one or more exception classes.
3It is used as part of method body.It is used with method signature.
4Checked exception cannot be propagated using throw only.Checked exception can be propagated with throws.
5We cannot throw multiple exceptions using throw.We can declare multiple exceptions using throws.