0
0
JavaComparisonBeginner · 4 min read

Throw vs throws in Java: Key Differences and Usage

In Java, throw is used to actually throw an exception object explicitly inside a method, while throws is used in a method signature to declare that the method might throw certain exceptions. throw handles a single exception instance, and throws declares one or more exception types that callers must handle.
⚖️

Quick Comparison

This table summarizes the main differences between throw and throws in Java.

Aspectthrowthrows
PurposeTo actually throw an exception objectTo declare exceptions a method can throw
Usage LocationInside method bodyIn method signature after parameter list
Number AllowedOne exception object at a timeMultiple exception types separated by commas
EffectTransfers control to exception handler immediatelyInforms caller to handle or declare exceptions
TypeUsed with an instance of ThrowableUsed with exception class names
Checked ExceptionsCan throw checked or unchecked exceptionsMust declare checked exceptions
⚖️

Key Differences

throw is a keyword used inside a method to actually throw an exception object. You create or use an existing exception instance and use throw to pass it to the runtime, which then looks for a matching catch block to handle it.

On the other hand, throws is used in a method declaration to specify that this method might throw certain exceptions. It does not throw exceptions itself but tells the caller that they must handle or further declare these exceptions. This is mainly for checked exceptions that the compiler enforces.

In summary, throw is about throwing an actual exception object, while throws is about declaring possible exceptions a method can throw to the caller.

⚖️

Code Comparison

Here is an example using throw to throw an exception inside a method.

java
public class ThrowExample {
    public static void checkAge(int age) {
        if (age < 18) {
            throw new IllegalArgumentException("Age must be 18 or older");
        } else {
            System.out.println("Access granted");
        }
    }

    public static void main(String[] args) {
        checkAge(16);
    }
}
Output
Exception in thread "main" java.lang.IllegalArgumentException: Age must be 18 or older at ThrowExample.checkAge(ThrowExample.java:4) at ThrowExample.main(ThrowExample.java:11)
↔️

throws Equivalent

This example shows throws used in a method signature to declare a checked exception that the caller must handle.

java
import java.io.*;

public class ThrowsExample {
    public static void readFile(String fileName) throws IOException {
        FileReader file = new FileReader(fileName);
        BufferedReader fileInput = new BufferedReader(file);
        System.out.println(fileInput.readLine());
        fileInput.close();
    }

    public static void main(String[] args) {
        try {
            readFile("test.txt");
        } catch (IOException e) {
            System.out.println("File not found or error reading file");
        }
    }
}
Output
File not found or error reading file
🎯

When to Use Which

Choose throw when you want to actually create and throw an exception object inside a method, especially for signaling an error immediately.

Choose throws when you want to declare that your method might throw certain checked exceptions, so callers know they must handle or declare them. This helps with compile-time safety.

In short, use throw to throw exceptions, and throws to declare them.

Key Takeaways

throw throws an actual exception object inside a method.
throws declares exceptions a method can throw to its caller.
throw is used with an exception instance; throws lists exception classes.
Use throws for checked exceptions to enforce handling at compile time.
Use throw to signal errors immediately by throwing exceptions.