What's the difference between throw and throws?
5 minbeginnerthrowthrowskeywords
Quick Answer
throw is a statement used inside a method body to actually raise a specific exception instance at that point in execution. throws is part of a method's signature, declaring which checked exception types that method might propagate to its callers, without itself raising anything.
Detailed Answer
These are easily confused by name, but serve completely different roles:
throw: a statement, used inside a method body, that actually raises (throws) a specific exception instance at that point:
void validate(int age) {
if (age < 0) {
throw new IllegalArgumentException("age cannot be negative"); // throw
}
}
throws: part of a method's signature, declaring which checked exception types the method might propagate to its caller — it doesn't raise anything itself, it's just a declaration/contract:
void readFile(String path) throws IOException, FileNotFoundException { // throws
// ...
}
A method can have a throws clause without ever executing a throw for that exact exception (e.g., it calls another method that declares it); conversely, a method can throw an unchecked exception without any throws declaration at all, since unchecked exceptions don't require it.
Mnemonic: throw is a verb (do it now); throws is a plural declaration (these might happen, be ready).