Java – IllegalArgumentException
IllegalArgumentException: Thrown when a method receives an illegal or inappropriate argument.
Example:
public class Test {
public static void checkAge(int age) {
if (age < 0) {
throw new IllegalArgumentException("Age cannot be negative");
}
}
public static void main(String[] args) {
try {
checkAge(-1);
} catch (IllegalArgumentException e) {
System.out.println("Caught IllegalArgumentException: " + e.getMessage());
}
}
}
No images available.