All About IllegalArgumentException Class in Java

IllegalArgumentException is thrown to indicate that a method has passed an illegal or inappropriate arguments while calling another method.It is a general purpose exception that can be thrown by a library that receives an illegal argument. We may choose to throw it by the method is accessed with wrong or illegal arguments.It can also occur if one attempts to pass a parameter that is not in valid range or value for a method.

The structure of this exception is as below:

public class java.lang.IllegalArgumentException extends java.lang.RuntimeException{
//constructor
public IllegalArgumentException();
//constructs an empty IllegalArgumentException object that is an object with no message specified.
public IllegalArgumentException(String message);
//constructs an IllegalArgumentException object with the message specified.

}
 
Example:

static double division(int myArg)
{
if (myArg==0)
throw new IllegalArgumentException("argument can not be zero for division");
else
return x/myArg;
}
 
All About IllegalArgumentException Class in Java All About IllegalArgumentException Class in Java Reviewed by Animesh Chatterjee on September 17, 2018 Rating: 5

No comments:

Powered by Blogger.