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 Overview

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.
Syntax:
public void myMethod() throws IOException, SQLException {
// code that might throw checked exceptions
}
Example:
import java.io.*;
public class ThrowsExample {
public static void readFile() throws FileNotFoundException {
FileReader file = new FileReader("file.txt"); // may throw exception
}
public static void main(String[] args) {
try {
readFile();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
Key Points:
throws
is used only in method signatures.- It tells the caller of the method that they need to handle or declare the exception.
- Commonly used for checked exceptions, not required for unchecked exceptions.
🚀 3. What is Throwable
in Java?
Throwable
is a class defined in the java.lang
package. It is the superclass for all exceptions and errors in Java.
public class Throwable extends Object implements Serializable
All exceptions (like IOException
, NullPointerException
) and errors (like OutOfMemoryError
, StackOverflowError
) are subclasses of Throwable
.
✅ Throwable Class Hierarchy:
java.lang.Object
└── java.lang.Throwable
├── java.lang.Exception
│ ├── IOException
│ └── SQLException
└── java.lang.Error
├── OutOfMemoryError
└── StackOverflowError
Example:
public class ThrowableExample {
public static void main(String[] args) {
try {
throw new Throwable("Custom throwable thrown");
} catch (Throwable t) {
System.out.println("Caught Throwable: " + t.getMessage());
}
}
}
Key Points:
- You can catch
Throwable
, but it’s usually discouraged. - Catching
Throwable
means catching both Exception and Error types, which could hide serious problems likeOutOfMemoryError
. - You should catch specific exceptions instead of
Throwable
in most cases.
📊 Comparison Table: throw
vs throws
vs Throwable

Practical Example Using All Three
import java.io.*;
public class ExceptionDemo {
// Method declares it might throw an IOException
public static void readFile() throws IOException {
throw new IOException("File not found!"); // Throwing manually
}
public static void main(String[] args) {
try {
readFile();
} catch (Throwable t) { // Catching using Throwable
System.out.println("Caught: " + t);
}
}
}
Output:
Caught: java.io.IOException: File not found!
This example uses:
throw
to manually raise an exception.throws
to declare the exception.Throwable
to catch any throwable object.
⚠️ Common Mistakes to Avoid
❌ 1. Using throw
without new
throw IOException; // ❌ Incorrect
throw new IOException(); // ✅ Correct
❌ 2. Catching Throwable instead of Exception
catch (Throwable t) { } // ❌ Only use this if you’re certain
catch (Exception e) { } // ✅ Prefer catching specific exceptions
❌ 3. Declaring throws
but not handling
public void riskyMethod() throws IOException {
// Must be handled where this method is called
}
Final Thoughts
To summarize:
- Use
throw
when you want to explicitly throw an exception from your code. - Use
throws
when a method might throw an exception, and you want the caller to handle it. Throwable
is the root class for all exceptions and errors, but you should avoid catching it unless absolutely necessary.
Comments
Post a Comment
Leave Comment