[Solved] java.lang.IllegalArgumentException in Java

java.lang.IllegalArgumentException is one of the most commonly occurred exceptions in Java. As the name suggests, this exception is thrown when one tries to pass illegal arguments to a method during runtime. In other words, the type of argument which is passed to the method is different or illegal as per the defined method.

For example, if the method requires a non-empty String as a parameter but we pass the input string as null then the java.lang.IllegalArgumentException is thrown since the input parameter can not be null.

Read Also: InputMismatchException in Java with Examples

IllegalArgumentException is an Unchecked Exception

This exception extends Runtime Exception and thus can be thrown during the operation of Java Virtual Machine (JVM). Due to this reason, it is also considered as a Runtime Exception or Unchecked Exception. As it is an unchecked exception, it needs not to be declared in the method’s or constructor’s throws clause.

What are the causes of java.lang.IllegalArgumentException?

java.lang.IllegalArgumentException is often thrown by programmers in order to avoid invalid arguments passed to a method.

1. Validation of Arguments within Range: We can use IllegalArgumentException to validate whether an argument belongs to a particular age or not.

Suppose we create a method that accepts age. If the user inputs negative age or age > 100, we can simply throw an IllegalArgumentException because it doesn’t fall in the range of 0 – 100.

import java.util.*;
class ExceptionDemo
{
    void ageDisplay(int age)
    {   
        if(age > 100 || age < 0)
            throw new IllegalArgumentException("Invalid Age");
        else
            System.out.println("Your Age is "+age+" years.");
    }
}

public class Main
{
        public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Your Age: ");
        int age = sc.nextInt();
        ExceptionDemo obj = new ExceptionDemo();
        obj.ageDisplay(age);
    }   
}

Now during runtime, if a user enters the correct age, his/her age would be displayed. Otherwise java.lang.IllegalArgumentException will be thrown.

Output:

Output 1:
Enter Your Age: 32
Your Age is 32 years.

Output 2:
Enter Your Age: 101
Exception in thread "main" java.lang.IllegalArgumentException: Invalid Age
        at ExceptionDemo.ageDisplay(exception.java:8)
        at Main.main(exception.java:22)

So, through the above example, we can validate whether an argument lies between a particular range or not.

2. Validation of Format of Argument:

One can throw java.lang.IllegalArgumentException if the format of an argument is illegal or invalid. Suppose a method that accepts date in the format "DD-MM-YYYY". If the user enters "DD/MM/YYYY" it’s invalid and thus IllegalArgumentException can be thrown.

3. Handling null values of Arguments:

java.lang.IllegalArgumentException can be used to validate whether arguments received by methods are not null.

How to resolve java.lang.IllegalArgumentException?

1. 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.

2. The IllegalArgumentException comes handy to avoid situations where your application's code would have to deal with unchecked input data.

3. We can also handle IllegalArgumentException using a try-catch block.

Constructor Summary

The IllegalArgumentException can be thrown in many ways depending upon the type of the argument passed to the constructor of IllegalArgumentExcepton class.

1. Constructs an IllegalArgumentException with no detail message.
IllegalArgumentException()


2. Constructs an IllegalArgumentException with a specified detail message.
IllegalArgumentException(String s)


That's all for today, please mention in comments in case you have any questions related to java.lang.IllegalArgumentException in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry