π Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 negativeKey Points:
- You can only throw objects that are instances of
Throwableor its subclasses. - You must
throwa new instance of an exception (throw new ...). - Once
throwis 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:
throwsis 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 SerializableAll 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
└── StackOverflowErrorExample:
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
Throwablemeans catching both Exception and Error types, which could hide serious problems likeOutOfMemoryError. - You should catch specific exceptions instead of
Throwablein 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:
throwto manually raise an exception.throwsto declare the exception.Throwableto 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
throwwhen you want to explicitly throw an exception from your code. - Use
throwswhen a method might throw an exception, and you want the caller to handle it. Throwableis the root class for all exceptions and errors, but you should avoid catching it unless absolutely necessary.
Comments
Post a Comment
Leave Comment