-

Wednesday, 8 May 2013

Example

 

package com.vinod;

import java.util.*;

public class ExceptionTest {

    public static void main(String[] args) {
        List al = new ArrayList();
        al.add("1");
        al.add("5");
        al.add("pretech");
        al.add("6");

//iterating arraylist and expecting NumberFormat exception


        for (int i = 0; i <= al.size() - 1; i++) {
            try {
                int a = Integer.parseInt((String) al.get(i));
                System.out.println("value :" + a);
            } catch (Exception e) {
                try {
                    throw e;
                } catch (Exception e1) {
                    System.out.println(e);

                }
            }
        }

    }

}

Output

value :1
value :5
java.lang.NumberFormatException: For input string: "pretech"
value :6

0 comments: