In Java, throw and throws are keywords used to manage exceptions. The throw keyword explicitly throws an exception, while throws declares exceptions that a method or constructor may propagate to its caller.
- throw transfers control to an exception handler or propagates the exception.
- throws specifies exception types in a method or constructor declaration.
throw Keyword
The throw keyword is used to explicitly throw an exception from a method or block of code. It supports both built-in and user-defined exceptions.
Syntax:
throw new ExceptionType("message");
class Geeks{
static void checkAge(int age){
if (age < 18) {
throw new IllegalArgumentException(
"Age must be 18 or above"
);
}
}
public static void main(String[] args){
try{
checkAge(15);
} catch (IllegalArgumentException e){
System.out.println("Exception caught: " + e.getMessage());
}
}
}
Output
Exception caught: Age must be 18 or above
Explanation: The checkAge() method explicitly throws an IllegalArgumentException when the age is below 18. The matching catch block handles the exception and prints its message.
throws Keyword
The throws keyword is used in a method or constructor declaration to specify exceptions that may be propagated to the caller.
- It is primarily used to declare checked exceptions.
- A caller must catch a checked exception or declare it using throws, unless it is already handled.
Syntax:
returnType methodName() throws ExceptionType{
// Method body
}
class Geeks{
static void waitForTask() throws InterruptedException{
Thread.sleep(100);
System.out.println("Task completed");
}
public static void main(String[] args){
try {
waitForTask();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("Task interrupted");
}
}
}
Output
Task completed
Explanation: The waitForTask() method declares InterruptedException using throws because Thread.sleep() may throw this checked exception. The calling method handles it using a try-catch block.
Note: If throws InterruptedException were removed from waitForTask() without catching the exception inside that method, the compiler would report an error similar to:
Using throw and throws Together
Both keywords can be used in the same program. The throws keyword declares a possible exception, while throw explicitly throws it.
Example: Program to demonstrate throw and throws together.
class Geeks{
static void validate(int age) throws Exception{
if (age < 18) {
throw new Exception("Not eligible");
}
}
public static void main(String[] args){
try {
validate(16);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Output
Not eligible
Explanation: The validate() method declares the checked exception using throws Exception and explicitly throws it using throw. The main method catches and handles the exception.
throw Vs throws
throw | throws |
|---|---|
Explicitly throws an exception. | Declares exceptions that may be propagated. |
Used inside a method or block. | Used in method or constructor declarations. |
Followed by a Throwable expression. | Followed by exception type names. |
Throws one exception object at a time. | Can declare multiple exception types separated by commas. |
Transfers control to exception-handling logic. | Does not itself throw an exception. |
Works with checked and unchecked exceptions. | Mainly used to declare checked exceptions. |