Member-only story
throw
vs throws
vs Throwable
In Java – Explained with Examples
🔒 This is a Medium member-only article. If you’re not a Medium member, you can read the full article for free on my blog: throw vs throws vs Throwable In Java.
In Java exception handling, three commonly confused terms are: throw
, throws
, and Throwable
. At first glance, these keywords and class names may seem related because they all deal with exceptions, but each plays a very different role.
If you’re new to Java or preparing for interviews, it’s essential to understand how throw
, throws
, and Throwable
work — and when to use each one correctly.
In this article, we’ll break down these three concepts with simple language, real code examples, and a comparison table to help you master Java exception handling.
📌 Quick Summary
Let’s now explore each one in detail.
🚀 1. What is throw
in Java?
The throw
keyword is used in Java to manually throw an exception. You can throw either checked or unchecked exceptions using throw
.
Syntax:
throw new ExceptionType("Error message");
Example:
public class ThrowExample {
public static void main(String[] args) {
int age = -1;
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
System.out.println("Age is valid");
}
}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: Age cannot be negative
Key Points:
- You can only throw objects that are instances of
Throwable
or its subclasses. - You must
throw
a new instance of an exception (throw new ...
). - Once
throw
is executed, the current method is immediately exited.
🚀 2. What is throws
in Java?
The throws
keyword is used in a method declaration to indicate that the method might throw a checked exception.
This is Java’s way of enforcing compile-time exception checking.