previous | index | next

Exception Handling and Command Line Arguments

The class below sums up integers from the command line and prints the total to standard output:
public class Adder {
    public static void main(String[] args) {
	int total = 0;
	for (String s : args) {
	    try {
		total += Integer.parseInt(s);
	    }
	    catch(NumberFormatException e) {
		System.out.println("Ignoring bad input: " + s);
	    }
	}
	System.out.println("The total is " + total);
    }
}

previous | index | next