Java throw Keyword
Usage and Examples
throw
Keyword in Java
In Java, exceptions can be thrown automatically by the JVM or manually by the programmer. The throw
keyword allows developers to deliberately trigger an exception. Why? Sometimes, you want to fail fast or validate input before continuing further. That's where throw
shines.
Syntax of throw
in Java
The throw
statement is used followed by an instance of an exception class:
throw new ExceptionType("Error message");
Example 1: Throwing a Built-in Exception
Let’s say you're writing a function that shouldn’t accept a negative age. Instead of letting the program crash somewhere down the line, you raise a red flag immediately.
public class Main {
static void validateAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
System.out.println("Age is valid: " + age);
}
public static void main(String[] args) {
validateAge(-5);
System.out.println("End of program");
}
}
Exception in thread "main" java.lang.IllegalArgumentException: Age cannot be negative
at Main.validateAge(Main.java:4)
at Main.main(Main.java:9)
What Happened?
We manually threw an IllegalArgumentException
when the age was invalid. As soon as it was thrown, the normal flow stopped, and the program exited before reaching the final print statement.
Example 2: Using throw
with Custom Exceptions
Sometimes, built-in exceptions aren't descriptive enough. You can create your own custom exceptions and throw them using the same throw
keyword.
// Define a custom exception
class LowBalanceException extends Exception {
public LowBalanceException(String message) {
super(message);
}
}
public class Bank {
static void withdraw(int amount) throws LowBalanceException {
if (amount > 1000) {
throw new LowBalanceException("Withdrawal amount exceeds limit");
}
System.out.println("Withdrawn: " + amount);
}
public static void main(String[] args) throws LowBalanceException {
withdraw(1500);
}
}
Exception in thread "main" LowBalanceException: Withdrawal amount exceeds limit
at Bank.withdraw(Bank.java:9)
at Bank.main(Bank.java:14)
Deep Dive: How throw
Works
- It throws an object — not a class name. That’s why you must use
new
. - It halts execution — control transfers to the nearest matching
catch
block or crashes the program if uncaught. - It can throw only objects of type
Throwable
— which includesException
andError
.
When to Use throw
Use it to:
- Enforce business rules (e.g., age must be > 18)
- Validate inputs
- Handle invalid states
- Fail fast and exit on unexpected conditions
Common Mistakes to Avoid
- Trying to throw a class instead of an object (e.g.,
throw IOException;
instead ofthrow new IOException();
) - Forgetting to declare
throws
in the method signature if the exception is checked - Throwing non-
Throwable
objects (likeString
orint
)
Real-Life Analogy
Think of throw
as pulling the fire alarm in a building. You're not solving the problem (that’s for try-catch
to handle), but you're raising the alert that something went wrong and needs attention. Immediate action is taken — either someone catches it or the whole system shuts down.