java.lang.IllegalArgumentException is runtime unchecked exception. IllegalArgumentException throw as a preconditions check to indicate that a method has been passed an illegal or inappropriate argument.
IllegalArgumentException occurred generally in two cases:
- IllegalArgumentException on preconditions check
- IllegalArgumentException on chained exception
Constructors
- IllegalArgumentException() :Constructs an IllegalArgumentException with no detail message.
- IllegalArgumentException(String s) :Constructs an IllegalArgumentException with the specified detail message.
- IllegalArgumentException(String message, Throwable cause) :Constructs a new exception with the specified detail message and cause.
- IllegalArgumentException(Throwable cause) :Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).
Example: IllegalArgumentException preconditions check
In below example validating percentage and email based on basic criteria and throwing generic message as “Bad Percentage” and “Invalid Email Address”.
public class IllegalArgumentExceptionExample { public static void main(String[] args) { try { validatePercentage(50); //valid percentage validatePercentage(-20);//invalida percentage } catch(Exception ex) { ex.printStackTrace(); } try { validateEmail("facingissuesonit@gmail.com"); //valid email validateEmail("facingissuesonit-gmail.com");//invalid email } catch(Exception ex) { ex.printStackTrace(); } } public static void validatePercentage(int pct) { if( pct < 0 || pct > 100) { throw new IllegalArgumentException("Bad Percent"); } } public static void validateEmail(String email) { if (!email.contains("@")) { throw new IllegalArgumentException("Invalid Email Address"); } } }
Output
java.lang.IllegalArgumentException: Bad Percent at com.exceptions.IllegalArgumentExceptionExample.validatePercentage(IllegalArgumentExceptionExample.java:44) at com.exceptions.IllegalArgumentExceptionExample.main(IllegalArgumentExceptionExample.java:23) java.lang.IllegalArgumentException: Invalid Email Address at com.exceptions.IllegalArgumentExceptionExample.validateEmail(IllegalArgumentExceptionExample.java:51) at com.exceptions.IllegalArgumentExceptionExample.main(IllegalArgumentExceptionExample.java:33)
Example: IllegalArgumentException chained exception
In below example, consider a situation in which a method throws an IllegalArgumentException with message “passing Argument is not valid” but the actual cause of exception was an ArithmeticException because of an attempt to divide by zero The method will throw only IllegalArgumentException to the caller. So the caller would not come to know about the actual cause of exception and will see only generic message.
public class ChainedExceptionExample { public static void main(String[] args) { try { int totalAge = 500; int numberOfPerson = 0; int averageAge = averageAge(totalAge, numberOfPerson); System.out.println("Average Age :" + averageAge); } catch (Exception ex) { System.out.println(ex); ex.printStackTrace(); } } public static int averageAge(int totalAge, int numberOfPerson) { int avarageAge; try { /** * ArithmaticException can happen here because of value value * NumberOfPerson as 0 */ avarageAge = totalAge / numberOfPerson; } catch (Exception ex) { System.out.println(ex); // Actual Exception /** * Exception Chaining here by relating this ArithmaticException to * IllegalArgumentException */ throw new IllegalArgumentException("Passing argument is not valid", ex.getCause()); } return avarageAge; } }
Output
java.lang.ArithmeticException: / by zero java.lang.IllegalArgumentException: Passing argument is not valid java.lang.IllegalArgumentException: Passing argument is not valid at com.customexceptions.ChainedExceptionExample.averageAge(ChainedExceptionExample.java:33) at com.customexceptions.ChainedExceptionExample.main(ChainedExceptionExample.java:10)
Know More
To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links: s
You must log in to post a comment.