Chapter 50: throw Keyword in Java
You already saw throw in passing when methods refused bad input. This chapter stays on throw itself: the statement that creates or forwards an exception object and stops the current method right there.
try / catch and finally were Chapters 48–49. throws on a method signature is Chapter 51.
1. Topic title
throw: hand the problem to whoever knows how to react—or let it bubble
2. What it means
throw must be followed by something whose type is Throwable (in practice almost always an Exception subtype). Execution of the current block ends at that line unless a try/catch around it catches the same type (or a supertype).
You can throw a fresh object:
throw new IllegalArgumentException("seat must be free");or rethrow what you caught after logging:
catch (IllegalStateException ex) {
System.err.println(ex.getMessage());
throw ex;
}3. Why it is used
Guard clauses at the top of a method keep the “happy path” readable: reject nonsense inputs early with throw, then assume the rest of the method sees only valid state.
4. Small picture
Think of throw as ringing a bell you cannot un-ring in that method: you are not returning a value; you are handing off control.
5. Java code example
public class ThrowDemo {
static void reserveSeat(boolean taken, String guest) {
if (guest == null || guest.isBlank()) {
throw new IllegalArgumentException("guest name required");
}
if (taken) {
throw new IllegalStateException("seat already held");
}
System.out.println("reserved for " + guest.trim());
}
public static void main(String[] args) {
try {
reserveSeat(false, " Ada ");
reserveSeat(true, "Bob");
} catch (IllegalArgumentException | IllegalStateException ex) {
System.out.println("could not book: " + ex.getMessage());
}
}
}The multi-catch line lists two unrelated exception types separated by |; inside the block, ex is effectively final and typed as their common supertype (Throwable pieces you can access are limited—here getMessage() is fine).
6. Explanation of code
First call succeeds and prints. Second call hits taken == true and throws before println. main catches both argument and state problems in one catch and prints a short reason.
7. Common mistakes
Throwing null — the runtime throws NullPointerException instead of your intent.
Throwing for ordinary branches (“not found” in a tight inner loop) when a boolean or Optional would read cleaner and cost less.
Messages without context — include the bad value (safely) so logs help later.
8. Best practices
Prefer specific exception types (IllegalArgumentException, IllegalStateException) over raw Exception when you choose the type yourself.
If you wrap another failure, use constructors that accept a cause (Chapter 48 already showed the idea).
9. Small practice task
Write static int dividePositives(int a, int b) that throws IllegalArgumentException if either argument is ≤ 0 or if b does not divide a evenly; otherwise returns a / b. Call it from main with try/catch and print friendly text.
Beginner tip
throw and throws (next chapter) look alike on the page but mean different things: throw is an action inside a method; throws is a note on the method signature about checked exceptions.