Add to Google Reader or Homepage

Exception in thread "main" java.lang.IllegalArgumentException

Illegal Argument Exception:

The illegal Argument Exception  is thrown when a method is passed an illegal value as an argument.
This kind of exceptions usually occurs in situations like when a user is prompted to enter string value as an input where he enters some other values rather than the intended one.

This exceptions can be resolved only by passing correct values as arguments.So this exceptions can be used to notify the user something abnormal was happened inside the program.


import java.lang.*;
import java.util.*;
public class Illegalarg1
public static void main(String args[])
String test="HELLO";
char in;
String index=null;
System.out.println("Enter the index:");
Scanner br =new Scanner(new InputStreamReader(;
index = br.nextLine();
try {
      in= test.charAt(Integer.parseInt(index));
      System.out.println("The char at the specified index is:"+in);
   catch ( IllegalArgumentException e ) {
      System.out.println("Illegal Argument Exception");
      System.out.println("Type the integer value for the index" );


Enter the index:
The char at the specified index is:L


Enter the index:
Illegal Argument Exception
Type the integer value for the index

In this above program the user is prompted to enter the integer value  as an argument but entering the char value results in a IllegalArgumentException.Thus this type of exceptions can be used to help the user to correct their mistakes.
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top