IllegalArgumentException


IllegalArgumentException

It is an unchecked exception a subclass of RuntimeException. It is thrown by Color constructor when wrong parameters are passed. Observe the syntax of java.awt.Color constructor.

Color clr1 = new Color(int red, int green, int blue);

The RGB values should be within the range of 0 to 255 (inclusive of both). If other values are passed Color constructor throws IllegalArgumentException.

Following is the hierarchy.

Object –> Throwable –> Exception –> RuntimeException –> IllegalArgumentException

Full hierarchy of exceptions is available at "Hierarchy of Exceptions".

Program on Exception
import java.awt.Color;
public class IAE
{
  public static void main(String args[])
  {
    Color clr1 = new Color(300, 150, 200);
  }
}

IllegalArgumentException
Output screen of IAE.java

Color clr1 = new Color(300, 150, 200);

In the above constructor, the red component value is given as 300 which is above 255. The constructor throws IllegalArgumentException. Observe the screenshot.

Many places this exception occurs in Java like when wrong parameter are passed to Font constructor or giving thread priority more than permissible value etc.

Leave a Comment

Your email address will not be published.