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.
Related Tutorials
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. Where is each keyword written?
throw goes inside a method or constructor body (or a switch expression branch). throws goes in the method or constructor declaration, after the parameter list.
Q7. Do unchecked exceptions need throws?
No. Only checked exceptions must be declared. Unchecked ones can be declared for documentation if you like.
Q8. Does throws handle an exception?
No. It only passes responsibility to the caller. Handling requires a catch block somewhere up the call stack.
Q9. What happens at runtime with throws?
Nothing special. throws is checked at compile time; at runtime only an actual throw (or a failing operation) produces an exception.
Q10. Can a constructor use both throw and throws?
Yes. A constructor can throw exceptions in its body and declare checked ones with throws.
Q11. Is throws Exception a good idea?
Rarely. It forces every caller to handle any exception and hides which errors really occur. Declare specific exception types.
Q12. How do I rethrow an exception?
Inside the catch block, write throw e;. If it is checked, the method must also declare it with throws.
Q13. Can interface methods use throws?
Yes. Implementations may then throw those checked exceptions, narrower ones or none at all.
Q14. What compile error appears when throws is missing?
"unreported exception X; must be caught or declared to be thrown".
Q15. Is throw a statement or an expression?
A statement. In switch expressions it can appear as the body of a case, but it still does not produce a value.
Q16. Can I throw an Error with throw?
Technically yes, since Error is a Throwable, but application code should normally only throw AssertionError for impossible situations.
Q17. Which keyword is used in a catch block to pass an exception on?
throw. The method then also needs throws if the exception is checked.
Q18. Are throw and throws used in other languages?
C# and C++ have throw but no checked-exception throws clause (C++ removed dynamic exception specifications). Java's checked exceptions make throws distinctive.
Q19. Can throws appear on a lambda?
No. A lambda takes its exception rules from the functional interface method it implements; to throw checked exceptions, that interface method must declare them.
Q20. Do I need throws for exceptions thrown inside a try block that catches them?
No. If the exception is caught within the method, it never leaves the method, so no declaration is needed.