Example to show Iterator exception in java

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

Example to show Iterator exception in java

     

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

IteratorException.java


import java.util.*;

public class IteratorException {

  public static void main(String[] args) {
  try {
  ArrayList List = new ArrayList();
  List.add("1");
  List.add("2");
  List.add("3");
  List.remove("34");

  Iterator itr = List.iterator();
  while (itr.hasNext()) {
  System.out.println(itr.next());
  }
  System.out.println(itr.next());
  catch (Exception ex) {
  System.out.println(ex);
  }
  }
}

 

Output of the program

1
2
3
java.util.NoSuchElementException


Download source code

 

Ads