public void setAge(int age) { if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); } this.age = age; }
public void processArray(int[] arr) { if (arr == null) { throw new IllegalArgumentException("Array cannot be null"); } // process the array }In conclusion, IllegalArgumentException is a part of the java.lang package library and is used to indicate that an invalid argument has been passed to a method. It is commonly used in defensive programming to handle input validation.