throw vs throws in Java with Example

In this blog post, we'll discuss the between throw vs throws in Java with examples.

1. throw Keyword

Definition:

The throw keyword is used to explicitly throw an exception from a method or any block of code. It's generally followed by an instance of the exception.

Usage:

  • You can use the throw keyword inside any method or constructor or code block. 
  • It's followed by an instance of Exception or a subclass thereof.

Example:

In this example, if the age is below 18, an exception is explicitly thrown.
public class ThrowExample {
    public static void checkAge(int age) {
        if (age < 18) {
            throw new ArithmeticException("Not eligible to vote.");
        } else {
            System.out.println("Eligible to vote.");
        }
    }

    public static void main(String[] args) {
        try {
            checkAge(15);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
}

Output:

java.lang.ArithmeticException: Not eligible to vote.
	at com.javaguides.net.ThrowExample.checkAge(ThrowExample.java:6)
	at com.javaguides.net.ThrowExample.main(ThrowExample.java:14)

In the above example, we created an instance of ArithmeticException and thrown using the throw keyword:

throw new ArithmeticException("Not eligible to vote.");

2. throws Keyword 

Definition: 

The throws keyword is used in the method signature to declare the exceptions that a particular method might throw but doesn't handle itself. By using this keyword, you're indicating to the callers of this method that they should be prepared to catch these exceptions.

Usage: 

It's used in the method's signature, after the method's parentheses, and before the opening brace of the method body. 

Multiple exceptions can be declared using a comma-separated list.

Example:

import java.io.*;

public class ThrowsExample {
    public static void readFile() throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("nonexistentfile.txt");
    }

    public static void main(String[] args) {
        try {
            readFile();
        } catch (FileNotFoundException e) {
            System.out.println("Caught exception: " + e.getMessage());
        }
    }
}

Output:

Caught exception: nonexistentfile.txt (No such file or directory)

Key Differences

Purpose: 

throw: Used to explicitly throw an exception. 

throws: Used to declare exceptions that a method might throw. 

Usage: 

throw: Inside the method or code block to indicate an exception occurrence.

throws: In the method signature indicate exceptions that the method might throw but does not handle. 

Followed By:

throw: It is followed by an instance of exception or any subclass of the Throwable class (e.g., new ExceptionName()).

throws: Exception class names, separated by commas (e.g., Exception1, Exception2).

Difference Between throw and throws in Java

Criteria throw throws
Purpose Used to explicitly throw an exception. Used to declare exceptions that a method might throw but doesn't handle.
Usage Location Inside the method or code block. Used in the method signature.
Followed By An instance of the exception (e.g., new ExceptionName()). Exception class names are separated by commas (e.g., Exception1, Exception2).
Keyword Type An executable statement. A part of method declaration/signature.
Effect Generates the exception immediately when executed. Specifies that the method might throw the listed exceptions during its execution.
Handling Exception needs to be caught or declared to be thrown. Allows the exception to propagate up the call chain to be handled elsewhere.
Examples throw new ArithmeticException("Error message"); void myMethod() throws IOException, SQLException

Comments