What is the difference between `throw` and `throws` in Java?
answer
- throw = statement, raises one instance now
- throws = signature clause, lists possible types
- throws enforces handle-or-declare for checked exceptions
- unchecked exceptions need no throws clause
- throw = verb/action; throws = contract/warning
basics
~20 sthrow is a statement that raises one exception right now inside a method body. throws is part of a method's signature that declares which checked exceptions the method might pass to its caller. One acts; the other warns.
solid answer
~40 sThey sound alike but do opposite jobs. `throw` is an executable statement used inside a method body: `throw new IOException("...")` actually raises a single exception object at that moment and transfers control. `throws` is a clause in the method signature: `void read() throws IOException` declares that the method may let a checked exception propagate out, forcing callers to either catch it or re-declare it. So `throw` is about *doing*; `throws` is about *advertising a contract*. You can have many `throw` statements, but a method declares its possible checked exceptions once in `throws`. Unchecked exceptions (RuntimeExceptions) can be thrown without being declared in `throws`. A common interview point: `throw` takes one Throwable instance; `throws` takes a comma-separated list of exception *types*.
go deeper
Correctly states throw is an action inside the body and throws is a signature declaration.
Explains the checked/unchecked distinction and the handle-or-declare rule that makes throws necessary.
Discusses API design tradeoffs of declaring checked exceptions and how unchecked ones bypass throws.
Weighs checked-vs-unchecked policy across a codebase or framework and its impact on caller ergonomics.
## Two keywords, one letter apart, opposite roles Java has two confusingly similar keywords. Knowing the difference is fundamental. ### `throw` — the action `throw` is a **statement** that lives **inside a method body**. It raises **one** exception object *right now*: ```java throw new IllegalArgumentException("id must be positive"); ``` The operand is a single **instance** of `Throwable` (or a subtype). When it runs, normal execution stops and the exception begins propagating. (See the separate question on the throw statement's mechanics.) ### `throws` — the declaration `throws` appears in the **method signature**, after the parameter list, and lists the **types** of *checked* exceptions the method may allow to escape to its caller: ```java void loadConfig(Path p) throws IOException, ParseException { // ... } ``` It is a **promise / warning**, not an action. It tells the compiler and the caller: "calling me might result in an IOException or ParseException coming out — be prepared." ## Checked vs. unchecked — why throws exists Java splits exceptions into: - **Checked exceptions** — subclasses of `Exception` but **not** `RuntimeException` (e.g. `IOException`). The compiler enforces the **handle-or-declare rule**: if your method can let one escape, you must either catch it or list it in `throws`. - **Unchecked exceptions** — `RuntimeException` and its subclasses (e.g. `NullPointerException`, `IllegalArgumentException`), plus `Error`. These do **not** need to appear in `throws`. So `throws` is essentially the mechanism that satisfies the compiler for checked exceptions. ## Putting them together ```java String first(List<String> xs) throws EmptyException { // throws: contract if (xs.isEmpty()) { throw new EmptyException("list is empty"); // throw: action } return xs.get(0); } ``` Here `throws EmptyException` declares the possibility; the `throw` statement is what actually realizes it. If `EmptyException` were a `RuntimeException`, the `throws` clause would be optional. ## Side-by-side | Aspect | `throw` | `throws` | |---|---|---| | Kind | Statement (in body) | Clause (in signature) | | Operand | One Throwable **instance** | Comma-separated exception **types** | | Effect | Raises an exception now | Declares what may propagate | | Count | Many per method | One clause, possibly many types | | Needed for unchecked? | Works for any | Optional for unchecked | ## Memory aid `throw` = the verb (you throw a ball). `throws` (with an s) = a label on the method ("this method *throws* IOExceptions") describing what it might do.
- Do you need a throws clause to throw a RuntimeException?No. RuntimeExceptions (and Errors) are unchecked, so the compiler does not require them to be declared in throws.
- Can a single method have both throw statements and a throws clause?Yes — commonly. The throws clause declares the checked exception the method may emit, and a throw statement inside the body actually raises it.
saying these in an interview costs you the question
- Using the terms interchangeably
- Thinking throws actually raises an exception
- Believing RuntimeExceptions must be listed in throws
- Saying throw can list multiple comma-separated types (that's throws)