throw and throws in Java
throw actually creates and raises an exception right at that point in the code — throw new IllegalArgumentException("bad input"); immediately stops normal execution and begins searching for a matching catch block, exactly as if that exception had been thrown by the JVM itself. throws, appearing in a method's signature, is an entirely different thing: it's a declaration, not an action, warning callers that this method might throw a particular checked exception during execution, so they know they'll need to either catch it or declare it themselves.
A method can declare several exception types after throws, comma-separated, and only checked exceptions actually require this declaration. The compiler enforces it, refusing to compile a method that can throw a checked exception without either declaring it or catching it internally; unchecked exceptions can be thrown freely with no such requirement.
throw
An executable statement that actually creates and raises a specific exception instance, right there in the code.
throws
A declaration in a method's signature, not an action, warning callers a checked exception might come out of this method.
Callers must respond to a declared checked exception
Either catching it themselves or declaring throws again to pass the obligation further up.
Multiple exceptions after throws
Comma-separated, if a method might throw more than one checked type.
Unchecked exceptions need no throws declaration
They can be thrown freely, with no compiler-enforced requirement to declare them.
public class ThrowDemo { static void validateAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); } System.out.println("Valid age: " + age); } public static void main(String[] args) { try { validateAge(-5); } catch (IllegalArgumentException e) { System.out.println("Caught: " + e.getMessage()); } }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.