import java.util.Scanner; /** Error3.java - This program illustrates the use of try/catch */ public class Error3 { public static void main(String [] args) { String [] a = { "8", "-2", "five", "0" }; // Let's traverse the array of strings, and convert each string // to an integer. The following loop is incorrect because it // will not catch the run-time error. // for (int i = 0; i < a.length; ++i) // { // int n = Integer.parseInt(a[i]); // } for (int i = 0; i < a.length; ++i) { int n = 0; try { n = Integer.parseInt(a[i]); System.out.println(n); } catch (NumberFormatException e) { System.out.println("This string is not a number."); } } } }