throw vs throws in Java – Key Differences with Examples
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
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
| Aspect | throw | throws |
|---|---|---|
| Purpose | Throws an exception | Declares exceptions a method may throw |
| Kind | A statement | Part of a method or constructor declaration |
| Where it appears | Inside a method body | After the parameter list in the method signature |
| Followed by | An exception object: throw new X(...) | Exception class names: throws X, Y |
| How many | One exception per statement | Several, separated by commas |
| Required when | You want to signal an error | A checked exception can leave the method |
| Effect | Stops normal execution immediately | No runtime effect; checked by the compiler |
| Unchecked exceptions | Can be thrown freely | Declaring 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:
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());
}
}
}
}
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:
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());
}
}
}
saved: report log: save failed (nothing to save) main handled: nothing to save
5. When to Use Which
- Use
throwwhen your code detects a problem: invalid input, broken business rule, impossible state. - Use
throwswhen 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 bethrow). - Writing
throw IOExceptionin a signature (should bethrows). - Throwing a checked exception without declaring it, which causes "unreported exception … must be caught or declared to be thrown".
- Declaring
throws Exceptioneverywhere instead of specific types.
7. Summary
throwthrows one exception object from inside a method.throwsdeclares, in the signature, which exceptions a method may throw.- Checked exceptions that leave a method must appear in its
throwsclause. - They are often used together:
throwraises the problem,throwswarns callers about it.
8. Official Java Documentation References
Frequently Asked Questions (FAQ)
Q1. What is the main difference between throw and throws?
throw actually throws an exception inside a method. throws declares, in the method signature, which exceptions the method might throw.
Q2. Can I use throw without throws?
Yes, for unchecked exceptions, or for checked exceptions that are caught inside the same method.
Q3. Can I use throws without throw?
Yes. A method that calls another method throwing a checked exception can declare it with throws without ever writing throw itself.
Q4. What follows throw and what follows throws?
throw is followed by an exception object, usually new SomeException(...). throws is followed by one or more exception class names.
Q5. Can throw throw several exceptions?
No, one per statement. throws can list several exception types.
Q6. What compile error appears when throws is missing?
"unreported exception X; must be caught or declared to be thrown".