Open In App

How to Solve IllegalArgumentException in Java?

Last Updated : 02 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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();
    }
}


Output

Welcome To GeeksforGeeks!


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads