throw vs throws
1. Introduction
In Java exception handling, the keywords throw and throws are used to work with exceptions, but they serve different purposes.
Both keywords are related to exception propagation, but they operate at different levels of the program.
throw→ used to explicitly throw an exception objectthrows→ used to declare that a method may throw an exception
Understanding the difference between these two keywords is important for writing robust and maintainable Java programs.
2. What is throw
The throw keyword is used to explicitly create and throw an exception object inside a method or block of code.
When throw is executed:
- an exception object is created
- the exception is thrown
- program execution stops at that line
- control transfers to the nearest exception handler
Syntax:
throw new ExceptionType("message");Example:
public class Demo {
public static void main(String[] args) {
int age = 15;
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
}
System.out.println("Eligible");
}
}Output:
Exception in thread "main" java.lang.ArithmeticException: Not eligible to voteExplanation:
- If the condition is true, the program throws an exception manually.
3. When to Use throw
The throw keyword is typically used when:
- validating input
- enforcing business rules
- detecting illegal program states
- manually triggering exceptions
Example: validating user input
public class Demo {
static void withdraw(int amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Invalid amount");
}
System.out.println("Withdrawal successful");
}
public static void main(String[] args) {
withdraw(-100);
}
}4. What is throws
The throws keyword is used in method declarations.
It tells the compiler and the caller that the method may throw certain exceptions.
Syntax:
returnType methodName() throws ExceptionTypeExample:
import java.io.FileReader;
public class Demo {
public static void readFile() throws Exception {
FileReader f = new FileReader("data.txt");
}
public static void main(String[] args) throws Exception {
readFile();
}
}Explanation:
readFile()may throw an exception.- The method declares this using
throws.
5. Why throws is Needed
Some exceptions are checked exceptions.
Checked exceptions must either be:
- handled using
try-catch, or - declared using
throws
Example:
import java.io.FileReader;
public class Demo {
public static void readFile() throws Exception {
FileReader f = new FileReader("data.txt");
}
}If throws is removed, the program will not compile because the compiler forces handling.
6. Example Showing throw and throws Together
Often both keywords are used in the same program.
Example:
public class Demo {
static void validate(int age) throws Exception {
if (age < 18) {
throw new Exception("Age must be 18 or above");
}
System.out.println("Access granted");
}
public static void main(String[] args) {
try {
validate(15);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}Explanation:
throwsdeclares the method might throw an exceptionthrowactually throws the exception
7. Key Differences Between throw and throws
| Feature | throw | throws |
|---|---|---|
| Purpose | Used to throw an exception object | Used to declare exceptions |
| Location | Inside method body | Method declaration |
| Usage | Throws a single exception object | Can declare multiple exceptions |
| Syntax | throw new Exception() | method() throws Exception |
| Function | Transfers control to exception handler | Warns caller about possible exceptions |
8. Multiple Exceptions with throws
A method can declare multiple exceptions.
Example:
public void processFile() throws IOException, SQLException {
// code
}This means the method may throw:
IOExceptionSQLException
The caller must handle or propagate them.
9. Throwing Custom Exceptions
The throw keyword is also used when throwing custom exceptions.
Example:
class InvalidAgeException extends Exception {
InvalidAgeException(String message) {
super(message);
}
}
public class Demo {
static void checkAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above");
}
}
}This is commonly used in enterprise applications.
10. Important Rules
Important points about throw and throws:
throwalways throws one exception object at a timethrowscan declare multiple exceptionsthrowis used inside methodsthrowsis used in method signatures- checked exceptions must be handled or declared
- unchecked exceptions do not require declaration
11. Best Practices
Good practices when using exceptions:
- Use
throwwhen enforcing business rules - Use
throwsto propagate checked exceptions - Avoid throwing generic
Exception - Prefer specific exception classes
- Always include meaningful error messages
Example:
throw new IllegalArgumentException("Age cannot be negative");12. Summary
throwis used to explicitly throw an exception object.throwsis used to declare exceptions that a method may produce.throwappears inside method bodies.throwsappears in method signatures.- Both keywords are important for managing exception flow in Java programs.
- Proper usage improves program reliability and error handling.
Written By: Shiva Srivastava
How is this guide?
Last updated on
