Difference Between Throw VS Throws in Java

Throw VS Throws in Java: In this article, we will discuss the difference between throw and throws clause in detail with a few examples.

Throw Clause or Throw Keyword

  • the throw keyword is used to throw an exception explicitly
  • It is used within the method to throw an exception explicitly
  • It is generally used for throwing user-defined exception or custom exception
  • Although, it is valid & possible to throw a 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 a user-defined exception
  • the throw keyword is always followed by instance (i.e.; instance of any type of exception)

Syntax:

throw instanceOfExceptionType;
  • throws keyword is used to declare an exception, indicating the caller method to handle the 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 the exception can be declared next to the method signature, with a comma (,) separating them
  • throws keyword is always followed by class (this class must be a pre-defined exception or user-defined exception which must be a subclass 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 a one-on-one parameter in the tabular format

Difference Between Throw VS Throws

Throw Clause/ Keyword Throws clause/keyword
the throw keyword is used to throw an exception explicitlythrows keyword is used to declare an exception to delegate/indicate exception handling the responsibility to caller method
the throw keyword is always followed by an instance of the Throwable type or exception typethrows keyword is always followed by an exception list (with a comma separating them)
the throw keyword is used within method i.e.; to throw an exception from try-catch block enclosed within a methodthrows keyword is used next to the 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 keywordThrown exception can be checked exception unchecked exception or a user-defined exceptionAny number of the exception can be declared (to be thrown) using throws keyword but they are all separated by a comma (,)

Example of Throw and Throws Keyword

  • Whenever checked exception (it may be a pre-defined or user-defined exception) is thrown explicitly using throw keyword, then it must be handled either using a try-catch block or throws clause. Therefore, we have used throws clause to delegate the exception responsibility to the caller method
  • But whenever unchecked exception (it may be a 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 a programmer to handle it

Example 1: checked exception

ThrowAndThrowsExample.java

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 an exception using throw keyword

ThrowWithUncheckedExceptionExample.java

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 Sgring");
        }
        // further processing with the string value
    }
}
 

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment