How to solve IllegalArgumentException in Java?
In this tutorial, we will learn the way to handle IllegalArgumentException in Java.
An IllegalArgumentException is thrown when a method is called with an argument that is not appropriate or illegal. To solve this issue, you need to find out what argument is causing the problem and why it is illegal.
Causes of java.lang.IllegalArgumentException
- When arguments are too far apart. The percentage, for instance, should range from 1 to 100.
- An IllegalArugmentExcpetion will be thrown if the user entered 101. In case of the improper argument format.
For instance, suppose the user passes YYYY-MM-DD instead of the date format that our method expects, which is YYYY/MM/DD. If our approach is unable to comprehend the situation, IllegalArugmentExcpetion will be raised. - When a method expects a non-empty string as a parameter but only receives a null string.
Implementation 1:
Let us see an example to understand the topic well.
File name: Student.java
public class Student {
int r;
public void setRanks(int ranks) {
if(ranks < 0 || ranks > 100)
throw new IllegalArgumentException(Integer.toString(ranks));
else
r = ranks;
}
public static void main(String[] args) {
Student s1 = new Student();
s1.setRanks(60);
System.out.println(s1.r);
Student s2 = new Student();
s2.setRanks(212);
System.out.println(s2.r);
}
}
Output:

Explanation: The above written code throws an IllegalArgumentException because the setRanks method checks if the value of ranks is within the range of 0 to 100. If the value is outside of this range, the method throws an IllegalArgumentException with the message set to the string representation of the argument ranks.
In the main method, two Student objects are created and their ranks are set using the setRanks method. The first object, s1, has its rank set to 60, which is within the acceptable range, so no exception is thrown. The second object, s2, has its rank set to 212, which is outside of the acceptable range, so an IllegalArgumentException will be thrown with the message "212".
The exception here will terminate the program and the second println statement will not be executed. To handle this exception, you can wrap the call to setRanks in a try-catch block, catch the IllegalArgumentException, and handle it appropriately, such as logging the error or displaying an error message to the user.
Implementation 2:
Let us take another example to understand the topic.
File name: Student.java
import java.util.Scanner;
public class Student {
public static void main(String[] args) {
String cont = "y";
run(cont);
}
static void run(String cont) {
Scanner scan = new Scanner(System.in);
while( cont.equalsIgnoreCase("y")) {
try {
System.out.println("Enter the roll no.: ");
int roll = scan.nextInt();
if (roll < 0 || roll > 100)
throw new IllegalArgumentException("Value must be non-negative and below 100");
System.out.println( roll);
}
catch(IllegalArgumentException i) {
System.out.println("Out of range. Press y if you wish to continue");
cont = scan.next();
if(cont.equalsIgnoreCase("Y"))
run(cont);
}
}
}
}
Output 1:

Output 2:

Explanation: The above-written code is a simple implementation of a program that takes input from a user, checks if the input value is within the range of 0 to 100, and throws an IllegalArgumentException if the value is outside of this range. The program uses a while loop to allow the user to continue entering values until they decide to exit.
The run method is where the main logic of the program resides. It uses a Scanner object to take input from the user. The method starts a while loop that continues as long as the user enters "y" or "Y". Within the loop, the user is prompted to enter a roll number. If the entered value is less than 0 or greater than 100, an IllegalArgumentException is thrown with a message indicating that the value must be non-negative and below 100. If the exception is thrown, the program prints a message asking the user if they want to continue and takes another input. If the user enters "y" or "Y", the program will call itself again to continue the loop. If the user enters any other value, the loop will exit and the program will terminate.
If the user enters a value within the range of 0 to 100, the value is printed and the loop continues, asking the user if they wish to continue.