Java - Throws and Throw: How to Handle Exceptions
Understand the difference between throws
and throw
in Java. Learn how to use throws
to declare exceptions in methods and throw
to explicitly throw an exception, enabling better exception handling in your Java applications.
Java - Throws and Throw | Throw an Exception
Java Throws and Throw
In Java, if a method does not handle a checked exception, it must declare it using the throws
keyword. This keyword is placed at the end of the method's signature.
You can throw an exception, either by creating a new instance or by using an exception that you have caught, with the throw
keyword.
It's important to understand the difference between throws
and throw
: throws
is used to postpone handling a checked exception, while throw
is used to explicitly invoke an exception.
Syntax
The syntax for throwing an exception using throws
and throw
is as follows:
Syntax
method(parameters) throws exception {
// Method implementation
throw new exception();
}
The following method declares that it throws a RemoteException
:
Example: Declaring RemoteException
import java.io.*;
public class ClassName {
public void deposit(double amount) throws RemoteException {
// Method implementation
throw new RemoteException();
}
// Remainder of class definition
}
A method can declare that it throws multiple exceptions by listing them, separated by commas. For instance:
Example: Declaring Multiple Exceptions
import java.io.*;
public class ClassName {
public void withdraw(double amount) throws RemoteException, InsufficientFundsException {
// Method implementation
}
// Remainder of class definition
}
Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.
Java Throws and Throw Example
The following example demonstrates the use of the throw
keyword to send an exception when an invalid argument is passed. We call a divide
method that checks if the second parameter is zero. If it is, it throws an IllegalArgumentException
with a custom message. Since IllegalArgumentException
is an unchecked exception, the divide
method is not required to declare a throws
statement. When the parent method does not handle the exception, the JVM intercepts it, prints the error message, and terminates the program.
Example: Throwing IllegalArgumentException
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) {
int a = 3;
int b = 0;
System.out.println("result: " + divide(a, b));
}
private static int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Second argument cannot be zero.");
}
return a / b;
}
}
Output
Exception in thread "main" java.lang.IllegalArgumentException: Second argument cannot be zero.
at com.tutorialsarena.ExcepTest.divide(ExcepTest.java:13)
at com.tutorialsarena.ExcepTest.main(ExcepTest.java:8)
More Examples
Example 1: Throwing an Exception for Invalid Arguments
This example shows the use of throw
and throws
keywords to send an exception when an invalid argument is passed and handle the exception. We call the divide
method which checks if the second parameter is zero, and if so, it throws an exception with a custom message. Since the Exception
is a checked exception, the divide
method must declare a throws
statement. In this case, the parent method handles the exception and prints the message.
Example: Handling Exception
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) {
int a = 3;
int b = 0;
try {
System.out.println("result: " + divide(a, b));
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
private static int divide(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("Second argument cannot be zero.");
}
return a / b;
}
}
Output
Exception: java.lang.Exception: Second argument cannot be zero.
Example 2: Using Throws and Throw in Main and Other Methods
This example illustrates the use of throw
and throws
keywords to throw an exception when an invalid argument is passed and the exception is not handled. We call the divide
method which checks if the second parameter is zero and throws an exception with a custom message. Since Exception
is a checked exception, the divide
method must declare a throws
statement. In this case, the parent method declares the exception, allowing the JVM to handle it.
Example: Declaring Throws in Main Method
package com.tutorialsarena;
public class ExcepTest {
public static void main(String args[]) throws Exception {
int a = 3;
int b = 0;
System.out.println("result: " + divide(a, b));
}
private static int divide(int a, int b) throws Exception {
if (b == 0) {
throw new Exception("Second argument cannot be zero.");
}
return a / b;
}
}
Output
Exception in thread "main" java.lang.Exception: Second argument cannot be zero.
at com.tutorialsarena.ExcepTest.divide(ExcepTest.java:15)
at com.tutorialsarena.ExcepTest.main(ExcepTest.java:9)