Throw vs. Throws
Overview
When working with Java exceptions, two closely named but completely different keywords are used: throw and throws. Confusing them is one of the most common mistakes for Java beginners.
`throw` is an action. You use it inside a method body to actively create and fire a specific exception object. The moment the JVM executes throw new IllegalArgumentException("Invalid input"), execution of the current method halts and the exception propagates up the call stack until something catches it.
`throws` is a warning label. You place it in the method signature (not the body) to declare to callers that this method might propagate one or more Checked Exceptions. It is a compiler-enforced contract telling whoever calls this method: "You must handle or further declare these exception types." It does NOT throw anything by itself — it is purely a declaration.
Syntax
import java.io.IOException;
public class FileService {
// 'throws' DECLARES that this method may produce an IOException.
// It forces the CALLER to handle it.
public String readFile(String path) throws IOException {
if (path == null) {
// 'throw' FIRES the exception right now, stopping execution.
throw new IllegalArgumentException("Path cannot be null");
}
// ... actual file reading logic that may throw IOException
return "file contents";
}
}
public class Main {
public static void main(String[] args) {
FileService service = new FileService();
// Because readFile() 'throws' IOException, the COMPILER
// forces us to handle it here!
try {
String content = service.readFile("data.txt");
} catch (IOException e) {
System.out.println("Could not read: " + e.getMessage());
}
}
}Common Pitfalls
- Declaring
throws RuntimeExceptionin the signature. Unchecked Exceptions do not need to be declared. Adding them to thethrowsclause clutters the code and gives a false impression that callers are required to handle them. - Throwing the root
Exceptionclass. Writingthrow new Exception("error")is too vague. Always throw the most specific exception that accurately represents the failure (e.g.,throw new InvalidUserIdException()). - Eating the original exception when re-throwing. If you catch an exception and throw a new one, always pass the original as the
cause:throw new ServiceException("message", originalException). Otherwise, the root cause is permanently lost, making debugging nearly impossible.
Interview Questions
throw and throws?throw is an executable statement used inside a method body to actually create and propagate an exception object at that exact moment. throws is a keyword used in the method signature to declare that the method may propagate specific Checked Exceptions, which the caller must then handle or further declare.
throw a Checked Exception without declaring it in the throws clause?No, for Checked Exceptions. If you throw new IOException() inside a method but do not declare throws IOException in the method signature, the compiler will throw a hard error. For Unchecked Exceptions (RuntimeExceptions), you can throw them without any throws declaration.
When catching an exception and rethrowing a new one, you pass the original exception as the constructor argument (the 'cause'). For example: catch (IOException e) { throw new ServiceException("Database failed", e); }. This preserves the full stack trace chain so developers can see the root cause.
Real-World Example
In a layered application (Controller → Service → Repository), each layer re-throws exceptions as its own type, wrapping the original as the cause. This gives the API a clean error response while preserving the full diagnostic chain for the logs.
// Repository Layer
public User fetchUser(int id) throws SQLException {
// Database call that could throw SQLException
}
// Service Layer
public User getUser(int id) throws UserNotFoundException {
try {
return repo.fetchUser(id);
} catch (SQLException e) {
// Chain the original exception as the cause!
throw new UserNotFoundException("User " + id + " not found", e);
}
}Check Your Knowledge
Test your understanding of Throw vs. Throws with these quick questions.