How to solve an IllegalArgumentException in Java?


An IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. This exception extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception and thus, it does not need to be declared in a method’s or a constructor’s throws clause.

Reasons for java.lang.IllegalArgumentException

  • When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArugmentExcpetion will be thrown.

  • When argument format is invalid. For example, if our method requires date format like YYYY/MM/DD but if the user is passing YYYY-MM-DD. Then our method can’t understand then IllegalArugmentExcpetion will be thrown.

  • When a method needs non-empty string as a parameter but the null string is passed.

Example

public class Student {
   int m;
   public void setMarks(int marks) {
      if(marks < 0 || marks > 100)
         throw new IllegalArgumentException(Integer.toString(marks));
      else
         m = marks;
   }
   public static void main(String[] args) {
      Student s1 = new Student();
      s1.setMarks(45);
      System.out.println(s1.m);
      Student s2 = new Student();
      s2.setMarks(101);
      System.out.println(s2.m);
   }
}

Output

45
Exception in thread "main" java.lang.IllegalArgumentException: 101
	at Student.setMarks(Student.java:5)
	at Student.main(Student.java:14)

Steps to solve IllegalArgumentException

  • When an IllegalArgumentException is thrown, we must check the call stack in Java’s stack trace and locate the method that produced the wrong argument.

  • The IllegalArgumentException is very useful and can be used to avoid situations where the application’s code would have to deal with unchecked input data.

  • The main use of this IllegalArgumentException is for validating the inputs coming from other users.

  • If we want to catch the IllegalArgumentException then we can use try-catch blocks. By doing like this we can handle some situations. Suppose in catch block if we put code to give another chance to the user to input again instead of stopping the execution especially in case of looping.

Example

Live Demo

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 an integer: ");
            int marks = scan.nextInt();
            if (marks < 0 || marks > 100)
               throw new IllegalArgumentException("value must be non-negative and below 100");
            System.out.println( marks);
         } catch(IllegalArgumentException i) {
            System.out.println("out of range encouneterd. Want to continue");
            cont = scan.next();
            if(cont.equalsIgnoreCase("Y"))
               run(cont);
         }
      }
   }
}

Output

Enter an integer:
1
1
Enter an integer:
100
100
Enter an integer:
150
out of range encouneterd. Want to continue
y
Enter an integer:

Updated on: 28-Nov-2023

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements