Java – throw v/s throws

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

1. 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;

2. throws-clause or throws 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

3. throw v/s throws:


throw clause/keywordthrows clause/keyword
throw keyword is used to throw exception explicitlythrows 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 typethrows 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 methodthrows 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 (,)

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

4.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
	}
}

4.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
	}
}

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Rules for Exception handling w.r.t Method Overriding
Java - throws keyword or clause