Home » Java Basics » exceptions » java.lang.IllegalArgumentException – How to solve Illegal Argument Exception

About Sotirios-Efstathios Maneas

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.

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

In this tutorial we will discuss about 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 to null, the IllegalArgumentException is thrown to indicate that the input parameter cannot be null.

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

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


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"));
		// The following command throws an IllegalArgumentException.
		System.out.println(IllegalArgumentExceptionExample.createRelativePath(null, "file1"));

A sample execution is shown below:

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)

How to deal with the 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.

Download the Eclipse Project

The Eclipse project of this example: IllegalArgumentExceptionExample.zip.

This was a tutorial about IllegalArgumentException in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.