How to Solve IllegalArgumentException in Java?
Last Updated :
02 Feb, 2021
An unexpected event occurring during the program execution is called an Exception. This can be caused due to several factors like invalid user input, network failure, memory limitations, trying to open a file that does not exist, etc.
If an exception occurs, an Exception object is generated, containing the Exception's whereabouts, name, and type. This must be handled by the program. If not handled, it gets past to the default Exception handler, resulting in an abnormal termination of the program.
IllegalArgumentException
The IllegalArgumentException is a subclass of java.lang.RuntimeException. RuntimeException, as the name suggests, occurs when the program is running. Hence, it is not checked at compile-time.
IllegalArgumentException Cause
When a method is passed illegal or unsuitable arguments, an IllegalArgumentException is thrown.
The program below has a separate thread that takes a pause and then tries to print a sentence. This pause is achieved using the sleep method that accepts the pause time in milliseconds. Java clearly defines that this time must be non-negative. Let us see the result of passing in a negative value.
Program to Demonstrate IllegalArgumentException:
Java
// Java program to demonstrate IllegalArgumentException
public class Main {
public static void main(String[] args)
{
// Create a simple Thread by
// implementing Runnable interface
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
// Try to make the thread sleep for -10
// milliseconds
Thread.sleep(-10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"Welcome To GeeksforGeeks!");
}
});
// Name the thread as "Test Thread"
t1.setName("Test Thread");
// Start the thread
t1.start();
}
}
Output:
Exception in thread "Test Thread" java.lang.IllegalArgumentException:
timeout value is negative
at java.base/java.lang.Thread.sleep(Native Method)
at Main$1.run(Main.java:19)
at java.base/java.lang.Thread.run(Thread.java:834)
In the above case, the Exception was not caught. Hence, the program terminated abruptly and the Stack Trace that was generated got printed.
Diagnostics & Solution
The Stack Trace is the ultimate resource for investigating the root cause of Exception issues. The above Stack Trace can be broken down as follows.

Part 1: This part names the Thread in which the Exception occurred. In our case, the Exception occurred in the “Test Thread”.
Part 2: This part names class of the Exception. An Exception object of the “java.lang.IllegalArgumentException” class is made in the above example.
Part 3: This part states the reason behind the occurrence of the Exception. In the above example, the Exception occurred because an illegal negative timeout value was used.
Part 4: This part lists all the method invocations leading up to the Exception occurrence, beginning with the method where the Exception first occurred. In the above example, the Exception first occurred at Thread.sleep() method.
From the above analysis, we reach the conclusion that an IllegalArgumentException occurred at the Thread.sleep() method because it was passed a negative timeout value. This information is sufficient for resolving the issue. Let us accordingly make changes in the above code and pass a positive timeout value.
Below is the implementation of the problem statement:
Java
// Java program to demonstrate Solution to
// IllegalArgumentException
public class Main {
public static void main(String[] args)
{
// Create a simple Thread by
// implementing Runnable interface
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
// Try to make the thread sleep for 10
// milliseconds
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"Welcome To GeeksforGeeks!");
}
});
// Name the thread as "Test Thread"
t1.setName("Test Thread");
// Start the thread
t1.start();
}
}
OutputWelcome To GeeksforGeeks!
Similar Reads
How to Solve java.lang.IllegalStateException in Java main Thread?
An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exception is caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is no
5 min read
java.io.FileNotFoundException in Java
java.io.FileNotFoundException which is a common exception which occurs while we try to access a file. FileNotFoundExcetion is thrown by constructors RandomAccessFile, FileInputStream, and FileOutputStream. FileNotFoundException occurs at runtime so it is a checked exception, we can handle this excep
4 min read
How to Handle a java.lang.ArithmeticException in Java?
In Java programming, the java.lang.ArithmeticException is an unchecked exception of arithmetic operations. This means you try to divisible by zero, which raises the runtime error. This error can be handled with the ArthmeticException. ArithmeticException can be defined as a runtime exception that ca
2 min read
How to Handle an IOException in Java?
An IOException in Java occurs when we try to perform some input or output tasks and then some issues occur. Programmers need to handle this issue explicitly with a piece of code that executes when an issue occurs. There is an entire class for handling such issues known as the IOException class, whic
3 min read
Java Program to Handle Checked Exception
Checked exceptions are the subclass of the Exception class. These types of exceptions need to be handled during the compile time of the program. These exceptions can be handled by the try-catch block or by using throws keyword otherwise the program will give a compilation error. Â ClassNotFoundExcept
5 min read
User Defined Exceptions using Constructors in Java
In Java, we have already defined, exception classes such as ArithmeticException, NullPointerException etc. These exceptions are already set to trigger on pre-defined conditions such as when you divide a number by zero it triggers ArithmeticException. In Java, we can create our own exception class an
3 min read
How to handle a java.lang.IndexOutOfBoundsException in Java?
In Java programming, IndexOutOfBoundsException is a runtime exception. It may occur when trying to access an index that is out of the bounds of an array. IndexOutOfBoundsException is defined as the RuntimeException. It can be used to find the out-of-bound run-time errors of an array. It is a part of
2 min read
How to Replace a Element in Java ArrayList?
To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the
2 min read
ClosedChannelException in Java with Examples
The class ClosedChannelException is invoked when an I/O operation is attempted on a closed channel or a channel that is closed to the attempted operation. That is if this exception is thrown, however, does not imply the channel is completely closed but is closed to the attempted operation. Syntax: p
4 min read
Java Program to Handle the Exception Methods
An unlikely event which disrupts the normal flow of the program is known as an Exception. Java Exception Handling is an object-oriented way to handle exceptions. When an error occurs during the execution of the program, an exception object is created which contains the information about the hierarch
4 min read