00:00

throw vs throws in Java – Key Differences with Examples

Last updated: 6 min read

throw and throws differ by one letter but do very different jobs. throw is an action: it actually throws an exception object at a specific point in the code. throws is a declaration: it appears in a method signature and warns callers which exceptions the method might throw.

This tutorial compares them side by side and shows how they are used together.

1. The Short Answer

throw and throws together
void withdraw(double amount) throws InsufficientFundsException {   // throws: "this may happen"
    if (amount > balance) {
        throw new InsufficientFundsException("not enough money");     // throw: "it is happening now"
    }
    balance -= amount;
}

2. throw vs throws: Comparison Table

Aspectthrowthrows
PurposeThrows an exceptionDeclares exceptions a method may throw
KindA statementPart of a method or constructor declaration
Where it appearsInside a method bodyAfter the parameter list in the method signature
Followed byAn exception object: throw new X(...)Exception class names: throws X, Y
How manyOne exception per statementSeveral, separated by commas
Required whenYou want to signal an errorA checked exception can leave the method
EffectStops normal execution immediatelyNo runtime effect; checked by the compiler
Unchecked exceptionsCan be thrown freelyDeclaring them is optional

3. Both Keywords in One Program

Here withdraw() throws a checked and an unchecked exception with throw, but only the checked one must be listed in its throws clause:

ThrowAndThrows.java
public class ThrowAndThrows {
    static class InsufficientFundsException extends Exception {
        InsufficientFundsException(String message) { super(message); }
    }

    static double balance = 1000;

    // throws: DECLARES that this method may throw the checked exception
    static void withdraw(double amount) throws InsufficientFundsException {
        if (amount <= 0) {
            throw new IllegalArgumentException("amount must be positive");     // throw: unchecked
        }
        if (amount > balance) {
            throw new InsufficientFundsException("balance " + balance + " is less than " + amount);   // throw: checked
        }
        balance -= amount;
        System.out.println("Withdrew " + amount + ", balance now " + balance);
    }

    public static void main(String[] args) {
        double[] requests = {300, 2000, -5};
        for (double r : requests) {
            try {
                withdraw(r);
            } catch (InsufficientFundsException e) {
                System.out.println("Declined: " + e.getMessage());
            } catch (IllegalArgumentException e) {
                System.out.println("Invalid request: " + e.getMessage());
            }
        }
    }
}
Output
Withdrew 300.0, balance now 700.0
Declined: balance 700.0 is less than 2000.0
Invalid request: amount must be positive

4. Rethrowing: throw Inside catch, throws on the Method

A method that catches an exception, logs it and rethrows it needs both keywords: throw e; in the catch block and throws in its signature, because the checked exception still leaves the method:

RethrowDemo.java
import java.io.IOException;

public class RethrowDemo {
    static void save(String data) throws IOException {               // throws: declaration
        if (data.isEmpty()) throw new IOException("nothing to save"); // throw: action
        System.out.println("saved: " + data);
    }

    static void saveWithLogging(String data) throws IOException {
        try {
            save(data);
        } catch (IOException e) {
            System.out.println("log: save failed (" + e.getMessage() + ")");
            throw e;                                                  // rethrow the same exception
        }
    }

    public static void main(String[] args) {
        try {
            saveWithLogging("report");
            saveWithLogging("");
        } catch (IOException e) {
            System.out.println("main handled: " + e.getMessage());
        }
    }
}
Output
saved: report
log: save failed (nothing to save)
main handled: nothing to save

5. When to Use Which

  • Use throw when your code detects a problem: invalid input, broken business rule, impossible state.
  • Use throws when a checked exception (thrown by you or by a method you call) is not handled inside the method and must be passed to the caller.
  • If you catch and fully handle a checked exception inside the method, you need neither.

6. Common Mistakes

  • Writing throws new Exception() in a method body (should be throw).
  • Writing throw IOException in a signature (should be throws).
  • Throwing a checked exception without declaring it, which causes "unreported exception … must be caught or declared to be thrown".
  • Declaring throws Exception everywhere instead of specific types.

7. Summary

  • throw throws one exception object from inside a method.
  • throws declares, in the signature, which exceptions a method may throw.
  • Checked exceptions that leave a method must appear in its throws clause.
  • They are often used together: throw raises the problem, throws warns callers about it.

Related Tutorials