How to solve “illegal argument exception” in java


What is “illegal argument exception”?
In Java, IllegalArgumentException is an exception that is thrown when a method receives an argument that is not valid or appropriate for its expected range or type. It indicates that the argument passed to the method is considered illegal or inappropriate.


Reasons for IllegalArgumentException:

1.    Invalid argument value: The method receives an argument value that is outside the acceptable range or violates a specific constraint defined by the method.

Solution: Ensure that the argument value is within the valid range or meets the required constraints. Validate the input before passing it to the method to avoid passing illegal values.

Example:

public void setAge(int age) {

    if (age < 0 || age > 150) {

        throw new IllegalArgumentException("Invalid age: " + age);

    }

    this.age = age;

}

2.    Null argument: The method expects a non-null argument, but a null value is passed, which is not allowed.

Solution: Check if the argument is allowed to be null or modify the method to handle null values. If null values are not permitted, throw an IllegalArgumentException explicitly when a null argument is encountered.

Example:

public void setName(String name) {

    if (name == null) {

        throw new IllegalArgumentException("Name cannot be null");

    }

    this.name = name;

}

3.    Incompatible argument type: The method expects an argument of a specific type, but a different type is provided, making it impossible to perform the required operations or conversions.

Solution: Ensure that the argument type matches the expected type. If needed, explicitly convert or cast the argument to the appropriate type before passing it to the method.

Example:

public void setPrice(double price) {

    if (!(price >= 0.0 && price <= 100.0)) {

        throw new IllegalArgumentException("Invalid price: " + price);

    }

    this.price = price;

}

4.    Illegal state or combination of arguments: The method receives a combination of arguments that is not allowed based on the current state or other arguments.

Solution: Review the method's requirements and constraints. Ensure that the provided arguments are in a valid state and that their combination adheres to the specified rules. Validate the arguments' state or combination and throw an IllegalArgumentException if they are illegal.

Example:

public void setDateRange(LocalDate startDate, LocalDate endDate) {

    if (endDate.isBefore(startDate)) {

        throw new IllegalArgumentException("End date cannot be before the start date");

    }

    this.startDate = startDate;

    this.endDate = endDate;

}


By addressing these reasons and applying the appropriate solutions, you can handle the IllegalArgumentException in Java. It is important to thoroughly validate the arguments passed to methods to ensure that they meet the expected criteria and to handle any invalid or inappropriate values gracefully.