throw v/s throws in Java

In this article, we will discuss difference between throw and throws clause in detail with few examples

Already discussed throw clause & throws clause in earlier articles, let us re-visit key points before going for difference between them

throw clause or throw keyword:

  • throw keyword is used to throw exception explicitly
  • It is used within method to throw exception explicitly
  • It is generally used for throwing user-defined exception or custom exception
  • Although, it is valid & possible to throw pre-defined exception or already defined exception in Java too
  • Maximum of only one exception can be thrown using throw keyword and it can be checked exception or unchecked exception or used-defined exception
  • throw keyword is always followed by instance (i.e.; instance of any type of exception)
  • Syntax:
    throw instanceOfExceptionType;
    

throws clause or throw keyword:

  • throws keyword is used to declare exception, indicating caller-method to handle exception whenever invoking
  • with the usage of throws clause, any type of exception can be declared (i.e.; checked exception or unchecked exception or user-defined exception)
  • Any number of exception can be declared next to method signature, with comma (,) separating them
  • throws keyword is always followed by class (this class must be pre-defined exception or user-defined exception which must be sub-class of Throwable class or one of its sub-class)
  • Syntax:
    access-modifier return-type method-name() throws exception-list;
    

 

Let us move on and discuss them on one-on-one parameter in the tabular format

throw v/s throws:

throw clause/keyword throws clause/keyword
throw keyword is used to throw exception explicitly throws keyword is used to declare exception
to delegate/indicate exception handling
responsibility to caller-method
throw keyword is always followed by instance of Throwable type or exception type throws keyword is always followed by
exception list (with comma separating
them)
throw keyword is used within method i.e.; to throw exception from try-catch block enclosed within method throws keyword is used next to
method signature
Syntax:

throw instanceOfExceptionType;
Syntax:

access-modifier 
return-type method-name() 
	throws exception-list;
Maximum of only one exception can be thrown using throw keyword

Thrown exception can be checked exception or unchecked exception or user-defined exception

Any number of exception can be declared
(to be thrown) using throws keyword

But they are all separated by comma (,)

 

Example on throw & throws keyword:

  1. Whenever checked-exception (it may be pre-defined or user-defined exception) is thrown explicitly using throw keyword, then it must be handled either using try-catch block or throws clause. Therefore, we have used throws clause to delegate the exception responsibility to caller-method
  2. But whenever unchecked-exception (it may be pre-defined or user-defined exception) is thrown explicitly using throw keyword, then it is not necessary to handle. It is up to the choice of programmer to handle it

Example 1: checked exception

ThrowAndThrowsExample.java

package in.bench.resources.exception.handling;

import java.io.FileNotFoundException;

public class ThrowAndThrowsExample {

	public static void main(String[] args) 
			throws FileNotFoundException {

		// must be surrounded with try-catch block compulsorily,
		// because we are invoking method throwing 
		// checked-exception OR throws clause
		printFileContent();
	}

	// throwing checked exception
	public static void printFileContent() 
			throws FileNotFoundException  {

		// assumed that, 
		// we are trying to access file from remote location
		// FileReader fileReader = 
		// new FileReader("D:/Folder/test.txt");
		throw new FileNotFoundException("File is not available");

		// further file processing
	}
}

 

Example 2: unchecked exception

  • Explicitly throwing exception using throw keyword

ThrowWithUncheckedExceptionExample.java

package in.bench.resources.exception.handling;

public class ThrowWithUncheckedExceptionExample {

	public static void main(String[] args) {

		// invoking method
		anotherMethod(null);
	}

	public static void anotherMethod(String str) {

		if(str == null){
			throw new NullPointerException("Please send some valid String");
		}

		// further processing with the string value
	}
}

 

References:

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Rules for Exception handling w.r.t Method Overriding in Java
throws keyword in Java