Solve IllegalArgumentException in Java



In Java, an IllegalArgumentException is thrown in order to indicate that a method has been passed an illegal argument. An illegal argument is one that does not meet the required input from user.

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

Some of the reasons for IllegalArgumentException in Java is as follows:

  • When Arguments out of range. For example, the percentage should lie between 1 to 100. If the user entered 101 then an IllegalArgumentException 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 the format, so IllegalArgumentException will be thrown.

  • When a method needs non-empty string as a parameter but the null string is passed. For example, if a method is expected to receive a meaningful string and the user passes an empty string (""), the method might throw an IllegalArgumentException.

Example

The following Java program will throw IllegalArgumentException when you run it:

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 of the above code is as follows:

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

Handling IllegalArgumentException in Java

Follow the steps given below to handle IllegalArgumentException in Java:

  • 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

The following example shows how to handle IllegalArgumentException in Java:

public class Student {
   public static void main(String[] args) {
      run();
   }

   static void run() {
      int marks = 85;

      try {
         if (marks < 0 || marks > 100)
            throw new IllegalArgumentException("Value must be non-negative and below 100");
         
         System.out.println("Marks: " + marks);
      } catch (IllegalArgumentException i) {
         System.out.println("Out of range encountered.");
      }
   }
}

On running, the above Java program displays the following output:

Marks: 85

Try a new value greater that 100 for marks variable. It will give the following result:

Out of range encountered.
Updated on: 2025-05-12T11:34:05+05:30

29K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements