exceptions

java.lang.IllegalArgumentException – How to solve Illegal Argument Exception

In this tutorial, we will discuss how to solve the java.lang.illegalargumentexception – IllegalArgumentException in Java.

This exception is thrown in order to indicate that a method has been passed an illegal or inappropriate argument. For example, if a method requires a non-empty string as a parameter and the input string equals null, the IllegalArgumentException is thrown to indicate that the input parameter cannot be null.

You can also check this tutorial in the following video:

java.lang.IllegalArgumentException – Video

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. Finally, the IllegalArgumentException exists since the first version of Java (1.0).

java.lang.IllegalArgumentException

1. The IllegalArgumentException in Java

The IllegalArgumentException is a good way of handling possible errors in your application’s code. This exception indicates that a method is called with incorrect input arguments. Then, the only thing you must do is correct the values of the input parameters. In order to achieve that, follow the call stack found in the stack trace and check which method produced the invalid argument.

The following example indicates a sample usage of the java.lang.IllegalArgumentException – IllegalArgumentException.

IllegalArgumentExceptionExample.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.File;
 
public class IllegalArgumentExceptionExample {
     
    /**
     *
     * @param parent, The path of the parent node.
     * @param filename, The filename of the current node.
     * @return The relative path to the current node, starting from the parent node.
     */
    public static String createRelativePath(String parent, String filename) {
        if(parent == null)
            throw new IllegalArgumentException("The parent path cannot be null!");
         
        if(filename == null)
            throw new IllegalArgumentException("The filename cannot be null!");
         
        return parent + File.separator + filename;
    }
     
    public static void main(String[] args) {
        // The following command will be successfully executed.
        System.out.println(IllegalArgumentExceptionExample.createRelativePath("dir1", "file1"));
        System.out.println();
         
        // The following command throws an IllegalArgumentException.
        System.out.println(IllegalArgumentExceptionExample.createRelativePath(null, "file1"));
    }
}

A sample execution is shown below:

dir1/file1
Exception in thread "main" 

java.lang.IllegalArgumentException: The parent path cannot be null!
	at main.java.IllegalArgumentExceptionExample.createRelativePath(IllegalArgumentExceptionExample.java:15)
	at main.java.IllegalArgumentExceptionExample.main(IllegalArgumentExceptionExample.java:29)

2. How to deal with the java.lang.IllegalArgumentException

  • When the IllegalArgumentException is thrown, you 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 your application’s code would have to deal with unchecked input data.

3. Download the Eclipse Project

 This was a tutorial about IllegalArgumentException in Java.

Last updated on Oct. 12th, 2021

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button