Java throw and throws Keyword
Exception handling is one of the most important features of Java. It helps developers handle runtime errors gracefully and keep applications stable.
Among Java’s exception-handling tools, throw and throws are often confusing for beginners.
In this article, we’ll clearly understand what throw and throws are, how they differ, and when to use each, with simple examples.
What is an Exception in Java?
An exception is an unwanted or unexpected event that occurs during the execution of a program and disrupts its normal flow.
Examples:
Dividing a number by zero
Accessing an invalid array index
Opening a file that does not exist
Java provides a powerful mechanism to handle these issues using exception handling.
Java throw Keyword
What is throw?
The throw keyword is used to explicitly create and throw an exception in a program.
It is mainly used when a programmer wants to manually signal an error condition.
Syntax of throw
throw new ExceptionType("Error message");Example of throw
public class ThrowExample {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
}
System.out.println("You can vote");
}
}Output
Exception in thread "main" java.lang.ArithmeticException: Not eligible to voteHere, we manually throw an exception when the condition is not satisfied.
Key Points about throw
Used inside a method or block
Throws one exception at a time
Used to create custom or predefined exceptions
Followed by an exception object
Java throws Keyword
What is throws?
The throws keyword is used in a method declaration to indicate that the method may pass an exception to the calling method.
Get Quipoin’s stories in your inbox
Join Medium for free to get updates from this writer.
It does not handle the exception — it only declares it.
Syntax of throws
returnType methodName() throws ExceptionType {
// code
}Example of throws
import java.io.*;
public class ThrowsExample {
static void readFile() throws IOException {
FileReader file = new FileReader("test.txt");
BufferedReader br = new BufferedReader(file);
br.readLine();
}
public static void main(String[] args) throws IOException {
readFile();
}
}Here, the exception responsibility is passed to the caller.
Key Points about throws
Used with method declaration
Can declare multiple exceptions
Mainly used for checked exceptions
Does not create an exception
throw vs throws – Simple Example Together
class Test {
static void checkNumber(int num) throws ArithmeticException {
if (num < 0) {
throw new ArithmeticException("Number cannot be negative");
}
System.out.println("Valid number");
}
public static void main(String[] args) {
checkNumber(-5);
}
}✔ throws declares the exception
✔ throw actually throws it
When to Use throw and throws?
Use throw when you want to manually trigger an exception
Use throws when you want the caller to handle the exception
Use both together for clean and reusable code
Real-World Use Case
In real applications:
throw is used in business logic validation
throws is used in service layers and APIs
This separation improves readability, debugging, and maintenance.
Conclusion
Understanding the difference between throw and throws is essential for writing clean and professional Java code.
throw → Creates and throws an exceptionthrows → Declares and passes an exception
Mastering these keywords will help you build robust, error-free Java applications.
Free Resources:
Video Lecture: https://youtu.be/mtEC1DYsX5Q?si=n_uI8X7-mUU_leRu
Exercise Practice: https://www.quipoin.com/exercise/java/throw-and-throws-keyword
Interview Practice: https://www.quipoin.com/interview/java/throw-and-throws-keyword
MCQs Practice: https://www.quipoin.com/practice-mcqs/java/throw-and-throws-keyword



