What is the difference between throw and throws in Java?
answer
- throw = action, one object, runtime
- throws = declaration, list of types, signature
- throw new X() vs ... throws X, Y
- Can declare without throwing; can throw unchecked without declaring
basics
~10 sthrow is a statement that actually raises an exception right now. throws is part of a method's signature that just declares which exceptions the method might pass to its caller. One acts, one announces.
solid answer
~40 sThey look similar but do opposite jobs. throw is an executable statement that raises a single exception object at that point in the code: throw new IllegalArgumentException("bad input"). throws appears in the method header and lists exception types the method may let propagate to callers: void parse() throws IOException, SQLException. So throw is imperative ('do it now, with this instance'), throws is declarative ('this method might emit these types'). throw takes one exception instance; throws takes a comma-separated list of types. A method can declare a checked exception in throws without ever executing a matching throw (e.g. it just calls another method that declares it), and conversely you can throw an unchecked exception without declaring it.
go deeper
Can state plainly that throw raises an exception and throws declares one, and give a small example of each.
Explains operands (instance vs. type list) and timing (runtime vs. compile-time), and shows a method declaring an exception it never literally throws because a callee does.
Connects the distinction to API design and exception-translation patterns, and notes that listing unchecked types in throws is documentation only.
Uses the distinction when defining error-handling conventions and signatures across modules, ensuring declared contracts match real failure modes.
## The two keywords Java has two similarly spelled keywords that beginners constantly confuse: **`throw`** and **`throws`**. They are not variants of each other; they operate at different places and times. ### `throw` — raise an exception now `throw` is a **statement** inside a method body. It immediately stops normal flow and hands a specific exception *object* to the runtime, which begins searching the call stack for a handler: ```java if (age < 0) { throw new IllegalArgumentException("age must be >= 0"); } ``` Key facts: - It takes **exactly one exception instance** (an object), not a type. - The expression after `throw` must evaluate to something assignable to `Throwable`. - It runs at execution time; reaching it is what 'throwing' means. ### `throws` — declare possible exceptions `throws` is part of the **method signature** (the header). It names the exception **types** that the method may let escape to its caller: ```java void loadFile() throws IOException, SQLException { ... } ``` Key facts: - It takes a **comma-separated list of types**, not instances. - It is a compile-time *contract*, not an action. - It is what powers the catch-or-declare rule for checked exceptions. ## How they relate A method might use `throw` directly: ```java void check() throws IOException { throw new IOException("disk full"); // throw raises it; throws declares it } ``` But a method can also declare an exception in `throws` *without* a literal `throw`, simply because it calls another method that declares it: ```java void run() throws IOException { loadFile(); // loadFile() declares IOException; no throw here, still declared } ``` And you can `throw` an unchecked exception with **no** `throws` declaration at all, because unchecked exceptions are exempt from declaration. ## Quick contrast table | Aspect | `throw` | `throws` | |---|---|---| | Kind | Statement (executable) | Clause in method header | | Operand | One exception **object** | List of exception **types** | | When | Runtime (raises it) | Compile-time (declares it) | | Purpose | Trigger an exception | Announce possible exceptions | ## Deriving the answer If asked, the crisp formulation is: *throw raises one exception instance at a point in execution; throws is a signature clause naming exception types a method may propagate.* Everything else follows from that distinction.
- Can a method declare 'throws IOException' but contain no throw statement?Yes. If it calls another method that declares IOException, the obligation propagates and the method may (or must, if not caught) declare it, even with no literal throw of its own.
saying these in an interview costs you the question
- Saying throw and throws are interchangeable
- Putting an instance in throws or a type list after throw
- Believing throws raises the exception
- Thinking every throws entry must correspond to a literal throw in that method