Hook: Interviewers love this one because a single word decides whether you are doing the throwing or only declaring that a method might throw.
Question: What is the difference between throw and throws in Java?
Answer: throw is used inside a method to actually send one exception object to the JVM. throws is used in a method or constructor signature to say, “this code may pass this checked exception to the caller.” In short: throw performs the action; throws announces the contract.
Interview-Ready Answer: I use throw when I want to create and raise a specific exception right now, like throw new IllegalArgumentException(...). I use throws in the method signature when I want to declare that a checked exception may escape to the caller, such as throws IOException. So the easy memory is: throw is the act, and throws is the promise.
Detailed Explanation: Think of throw as the moment the problem is raised, and throws as the method saying, “I might let this problem bubble up.” A throw happens at runtime; a throws clause is mainly a compile-time promise.
new.throw immediately hands that object to the JVM and stops the current method’s normal flow.catch.throws does not throw anything by itself. It only appears in a method or constructor signature.throws.throw when the current method detects bad input, illegal state, or a condition it cannot continue with.throws when a method cannot fully handle a checked problem such as file access, network access, or a database read.IllegalArgumentException for programmer mistakes or invalid state that the caller should not be forced to handle.| Aspect | throw | throws |
|---|---|---|
| Meaning | Actually raise | Declare possible |
| Location | Method body | Signature |
| Runtime effect | Stops flow | No direct effect |
| Compile-time role | Type checked | Checked exceptions |
| Example | throw new IOException() | throws IOException |
Normal execution with throws has essentially no runtime cost. Throwing is much more expensive than an if branch: object creation is roughly O(1), but stack unwinding is O(d), where d is the number of stack frames between the throw site and the handler. In real code, that can feel like microseconds to low milliseconds, depending on stack depth and how much stack trace data is collected. That is why exceptions are for unusual situations, not everyday control flow.
throw must be followed by a Throwable object, not a primitive or a string.throws can be used on methods and constructors, not on fields or local variables.throws, separated by commas.throw null; compiles, but it ends in a NullPointerException at runtime.Memory guide: throw is the alarm button you press; throws is the sign that says the alarm might ring.
In an e-commerce checkout service, one method validates user input and another calls a tax API. The input validator uses throw to reject an empty postal code immediately, because the request is already invalid. The tax client uses throws IOException because it cannot fix a network failure itself; it must let the caller decide whether to retry, fail fast, or fall back.
What goes wrong when someone mixes them up? A developer may swallow a checked exception in a catch-all block, or forget to declare it and then wrap everything in a vague runtime exception. In production, users start seeing generic checkout failures, logs lose the original cause, and retry traffic spikes because a temporary network problem is being treated like a business-rule bug.
The symptom pattern is very recognizable: sudden 500 errors, stack traces that end too early, and support tickets saying orders cannot complete even though cart validation looks fine. The fix is simple but important: use throw to signal the exact bad condition, and use throws to keep the checked error visible until the right layer handles it.
public class ThrowVsThrowsDemo {
// A checked exception: callers must catch it or declare it.
static class AgeFormatException extends Exception {
AgeFormatException(String message) {
super(message);
}
}
private static int parseAge(String rawAge) throws AgeFormatException {
// throw is the action: we stop here if the input is missing.
if (rawAge == null || rawAge.isBlank()) {
throw new AgeFormatException("Age is required.");
}
try {
return Integer.parseInt(rawAge.trim());
} catch (NumberFormatException e) {
// We convert a low-level parsing error into a domain-specific checked exception.
throw new AgeFormatException("Age must be a whole number: " + rawAge);
}
}
private static void registerUser(String rawAge) throws AgeFormatException {
int age = parseAge(rawAge); // throws in the signature lets this method pass the checked error upward
if (age < 18) {
// Unchecked exception: no throws clause required, because this is a business-rule violation.
throw new IllegalArgumentException("User must be at least 18, but got " + age);
}
System.out.println("Accepted age: " + age);
}
public static void main(String[] args) {
String[] samples = { "21", "16", "abc", null, " 30 " };
for (String sample : samples) {
try {
System.out.println("Input: " + sample);
registerUser(sample);
} catch (AgeFormatException e) {
System.out.println("Handled checked exception: " + e.getMessage());
} catch (IllegalArgumentException e) {
System.out.println("Handled unchecked exception: " + e.getMessage());
}
System.out.println("---");
}
}
}Follow-up & Tricky Questions:
throws be used with runtime exceptions? Yes, you can declare them, but the compiler does not require it. It is usually optional and mainly used for documentation or API design clarity.RuntimeException and do not have to be declared.throws? Yes. You can write multiple types separated by commas, but inside the method body each throw still throws only one exception object at a time.throws clause? Yes, but only by narrowing it or removing checked exceptions. It cannot add broader checked exceptions than the parent method.throw a method? No. It is a Java statement, so it lives inside method or constructor bodies.throw accept an exception class name? No. It must be an exception object, such as new IOException(...).throws appear inside a method body? No. It belongs in the signature only, right after the parameter list.Common Mistakes:
throw is used inside the method body; throws is used in the method or constructor signature.throw a class or a primitive. Correction: you must throw an object that extends Throwable, such as new Exception(...).Memory Hook: throw is the fire alarm button; throws is the sign on the wall that says the alarm may ring.
Cheat Sheet:
throw = actually raise one exception object.throws = declare possible checked exceptions.throw goes in the body; throws goes in the signature.Practice Tasks:
registerUser also rejects ages above 120 with throw.throws.IOException-style thinking: write a method that declares a checked exception and another that handles it one layer up.