Learn Java
import com.learnjava.exceptionhandling.ThrowThrows;

throw and throws in Java

@Concept

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.

@HowItWorks
1

throw

An executable statement that actually creates and raises a specific exception instance, right there in the code.

2

throws

A declaration in a method's signature, not an action, warning callers a checked exception might come out of this method.

3

Callers must respond to a declared checked exception

Either catching it themselves or declaring throws again to pass the obligation further up.

4

Multiple exceptions after throws

Comma-separated, if a method might throw more than one checked type.

5

Unchecked exceptions need no throws declaration

They can be thrown freely, with no compiler-enforced requirement to declare them.

@Example
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());
}
}
}
Output
Caught: Age cannot be negative
1validateAge(-5) checks its argument and finds it invalid, since -5 is less than 0.
2throw new IllegalArgumentException(...) actually creates and raises that exception right there, immediately exiting the method.
3The println for a valid age never runs at all, since the throw happened before reaching it.
4main's catch block catches the raised exception and prints its message, since IllegalArgumentException is unchecked and needed no throws declaration on validateAge at all.
@UseCases
Rejecting an invalid argument immediately with a clear, specific exception and message
Declaring that a method might throw a checked exception, like IOException, so callers know to handle it
Understanding why a checked exception forces callers to catch it or declare it themselves, while an unchecked one doesn't
Writing validation logic that fails fast with a descriptive exception instead of continuing with bad data

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.