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



Home Java Java-exception Example to show Collection exception in java

Related Tutorials


 
 

Share on Google+Share on Google+

Example to show Collection exception in java

Advertisement
Here we are describing the use of using exception class in java .Exceptions are the way in Java to indicate to a calling method that an abnormal condition has occurred.

Example to show Collection exception in java

     

Here we are describing the use of using exception class in java .Exceptions are the way in Java to indicate to a calling method that an abnormal condition has occurred. This tutorial describes  how to handle collection exceptions appropriately in your programs and designs. The steps involved in the program are described below:-

 

 

 

 

 

CollectionException.java

import java.util.*;

public class CollectionException {

  public static void main(String[] args) {
  try{
  ArrayList List = new ArrayList();
  List.add("1");
  List.add("2");
  List.add("3");
  List.remove(4);
  for (int index =10; index < List.size(); index++) {
  System.out.println(List.get(index));
  }}
  catch(Exception ex){
  System.out.println("Please check the list, element does not exists"
  +ex.getMessage());
  }
  }
}

Output of the program

Please check the list, 
element does not existsIndex: 4, Size: 3

 

Note:-To correct the exception you have to give the specified index that exists in the Array list.  

Download source code

 

Advertisement

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 Collection exception in java   View All Comments

Post your Comment


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