In Java, throw is a statement that actually raises an exception at runtime, while throws is a clause in a method signature that declares which checked exceptions the method may pass on to its caller. You write throw new IllegalArgumentException("bad input") inside a method body, and void read() throws IOException in the method header.
The two work together: throw creates the problem, throws tells callers and the compiler what to expect. The compiler enforces the throws declaration only for checked exceptions.
throw takes a single Throwable object and immediately stops normal execution of the current method. The JVM then looks for a matching catch block up the call stack.
Rules for throw:
Throwable or a subclass. Throwing null causes a NullPointerException.throw in the same block is unreachable and does not compile.throws clause when the exception is unchecked (a RuntimeException or Error).throws is part of the method declaration. It lists checked exception types that the method does not handle itself:
A caller of readConfig must either catch IOException or declare it with its own throws. If it does neither, the code does not compile. A method may list several exceptions, separated by commas: throws IOException, SQLException.
| Kind | Examples | Must be caught or declared? |
|---|---|---|
| Checked | IOException, SQLException, InterruptedException | Yes |
| Unchecked (runtime) | IllegalArgumentException, NullPointerException, IllegalStateException | No |
| Errors | OutOfMemoryError, StackOverflowError | No, and usually should not be caught |
Use checked exceptions for conditions a caller can reasonably recover from, such as a missing file. Use unchecked exceptions for programming errors such as invalid arguments. Many modern libraries, including Spring, favour unchecked exceptions to keep signatures clean.
A common pattern is to detect a problem with throw and declare it with throws:
Because InvalidAgeException extends Exception (not RuntimeException), it is checked, so checkAge has to declare it.
When you catch a low-level exception and throw a higher-level one, pass the original as the cause so the stack trace is not lost:
Other good practices:
Exception or Throwable just to ignore it.AutoCloseable, so cleanup does not depend on finally.| Aspect | throw | throws |
|---|---|---|
| What it is | Statement | Clause in a method or constructor signature |
| Where | Inside a method body | After the parameter list |
| Operand | One exception object | One or more exception class names |
| Effect | Raises the exception now | Declares the possibility to callers |
| Count | One exception per statement | Multiple types allowed |
Yes. A method can declare throws IOException because it calls another method that throws it, without ever using throw itself.
No. Unchecked exceptions do not have to be declared, although declaring them can document intent, and Javadoc @throws is a good place to describe them.
Yes. Constructors can declare checked exceptions the same way methods do.
It propagates up the call stack. If it reaches the top of the thread, the thread terminates and the uncaught exception handler prints the stack trace. In main, that ends the program with a non-zero exit code.
throw raises one exception object at runtime; throws declares the checked exceptions a method may pass to its callers. Use checked exceptions for recoverable conditions and unchecked ones for programming errors, wrap low-level exceptions with a cause, and keep exception types specific and meaningful.