← Back to Java

throw vs throws

JavaEntryjava

The Question

What is the difference between throw and throws in Java?

What a Strong Answer Covers

  • throw = explicitly throw exception inside method body
  • throws = declare exceptions in method signature
  • throw single instance
  • throws multiple classes
  • throws propagates to caller

Senior-Level Answer

`throw` and `throws` are both part of Java's exception mechanism but operate at different points and serve different purposes.

`throw` is a statement used inside a method body to explicitly raise an exception. It takes an instance of `Throwable` (typically an `Exception` or `RuntimeException` subclass). Execution immediately leaves the current block and the stack unwinds looking for a matching `catch`. Example: `throw new IllegalArgumentException("value must be positive");`

`throws` is a clause in a method signature that declares which checked exceptions the method might propagate to its callers. It is a contract declaration, not an execution command. Any caller must either catch those exceptions or declare them in its own `throws` clause, propagating the obligation up the call stack. Example: `public void readFile(String path) throws IOException, ParseException {}`

The relationship: if a method uses `throw` to raise a checked exception, it must either handle that exception internally with `try-catch` or declare it with `throws`. Unchecked exceptions (`RuntimeException` subclasses) and errors can be thrown without a `throws` declaration.

Key distinctions: - `throw` operates at runtime; `throws` is a compile-time declaration. - `throw` takes a single object instance; `throws` lists one or more exception types (comma-separated). - `throw` causes program flow to change; `throws` is informational — it doesn't cause anything to happen on its own.

A well-designed API uses checked exceptions (declared with `throws`) for recoverable conditions callers are expected to handle, and unchecked exceptions for programming errors. Overusing checked exceptions forces callers into boilerplate handling that often just wraps and rethrows, which is a design smell.

Key Differences

Aspectthrowthrows
TypeStatementMethod declaration keyword
Used inMethod/constructor bodyMethod/constructor signature
TakesA Throwable instanceOne or more exception class names
PurposeRaises an exception at runtimeDeclares possible checked exceptions to callers
Required forAny exception you want to raiseChecked exceptions that aren't caught internally
Affects control flowYes — immediatelyNo — purely declarative

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly distinguishes throw (raises exception) from throws (declares exceptions) with an accurate example of each.

3/3 — Strong Answer

Also explains checked vs unchecked exception rules, why throws is not required for RuntimeException, and the API design guidance for when to use checked vs unchecked exceptions.

Common Mistakes

  • Thinking throws causes an exception to be thrown — it only declares that one might be thrown; execution is unchanged by the declaration alone.
  • Forgetting that RuntimeException subclasses don't need throws declarations even if they are thrown.
  • Conflating throws with a guarantee — a method declaring throws IOException may not always throw it.

Follow-Up Questions

  • Can a method declare throws for a RuntimeException? Is that useful? — Yes, it's legal but not enforced by the compiler. It can serve as documentation, but it doesn't compel callers to handle it.
  • What happens if you catch a checked exception that the compiler knows can never be thrown? — Compile error in Java — unreachable catch clauses for checked exceptions are flagged, unlike unchecked ones.
  • When should you choose a checked exception over an unchecked exception in API design? — Checked: recoverable conditions callers are expected to handle (IOException). Unchecked: programming errors, invalid state, contract violations.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout