Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-exception Example to show ArraylistException in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Example to show ArraylistException in Java

Advertisement
Here we are describing the use of using exception class in java .This tutorial describes how to handle Array list exceptions appropriately in your programs and designs.

Example to show ArraylistException in Java

     

Here we are describing the use of using exception class in java .This tutorial describes  how to handle Array list exceptions appropriately in your programs and designs. The steps involved in the program are described below:-

ArraylistException.java

import java.util.*;
public class ArraylistException {
  void buildAlphabet() {
  ArrayList list = new ArrayList();

  for (int i =1; i <=5; i++)
  {
  list.add(i,(String)("Girish"));
  }
  System.out.print("Elements of array are: " + list);
  }

  public static void main(String[] args) {
  ArraylistException ale=new ArraylistException();
  ale.buildAlphabet();
  }
}


Output of the program

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:367)
at ArraylistException.buildAlphabet(ArraylistException.java:11)
at ArraylistException.main(ArraylistException.java:18)
Java Result: 1

 
To avoid this error you have to give proper index for the List means to say starting index which is 0 for the array not 1.

Download SourceCode

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Related Tutorials

Discuss: Example to show ArraylistException in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com