What is the difference between the throw and throws keywords in Java?

The throw keyword is used to explicitly throw an exception from a method or a block of code, while the throws keyword is used in a method signature to declare the types of exceptions that the method might throw. Here’s how they differ:

  • throw keyword: It is used within a method to throw a specific exception explicitly. It is followed by an instance of an exception class that is thrown.
  • throws keyword: It is used in the method declaration to indicate the checked exceptions that the method can potentially throw. It is followed by the names of the exception classes separated by commas.

Methods using the throws keyword either handle the exception internally using try-catch or pass the responsibility

Code Example

import java.io.IOException;

class Example {
    // Using the throws keyword to declare a checked exception
    public static void methodWithCheckedException() throws IOException {
        throw new IOException("This is a checked exception.");
    }
    
    // Using the throw keyword to throw an unchecked exception
    public static void methodWithUncheckedException() {
        throw new NullPointerException("This is an unchecked exception.");
    }
    
    public static void main(String[] args) {
        try {
            methodWithCheckedException();
        } catch (IOException e) {
            System.out.println("Caught checked exception: " + e.getMessage());
        }
        
        try {
            methodWithUncheckedException();
        } catch (NullPointerException e) {
            System.out.println("Caught unchecked exception: " + e.getMessage());
        }
    }
}
Code Explanation

In this example, we have a class called Example with two methods: methodWithCheckedException() and methodWithUncheckedException().

The methodWithCheckedException() method uses the throws keyword in its method signature to declare that it can potentially throw a checked exception, specifically IOException. Inside the method, we create and throw an IOException using the throw keyword.

The methodWithUncheckedException() method does not use the throws keyword in its method signature because it throws an unchecked exception, specifically NullPointerException. Inside the method, we directly throw a NullPointerException using the throw keyword.

In the main() method, we demonstrate the usage of both methods. We invoke methodWithCheckedException() inside a try-catch block and catch the IOException that is thrown. We print a message indicating that a checked exception was caught.

Next, we invoke methodWithUncheckedException() inside a try-catch block and catch the NullPointerException that is thrown. We print a message indicating that an unchecked exception was caught.

The difference between the throw keyword and the throws keyword is as follows:

  • throw keyword: It is used to explicitly throw an exception from a method or a block of code. We use it to create and throw specific exceptions, whether they are checked or unchecked.
  • throws keyword: It is used in the method declaration to indicate the checked exceptions that the method can potentially throw. It declares the types of exceptions that may occur within the method. By using the throws keyword, we inform the caller of the method about the possible exceptions they need to handle or propagate.

In the example, methodWithCheckedException() declares that it may throw IOException using the throws keyword, while methodWithUncheckedException() does not declare any exceptions in its method signature because it throws an unchecked exception.