Specifications

Sun Services
Java™ Programming Language
Module 8, slide 8 of 25
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
The try-catch Statement
1 public class AddArguments3 {
2 public static void main(String args[]) {
3 int sum = 0;
4 for ( String arg : args ) {
5 try {
6 sum += Integer.parseInt(arg);
7 } catch (NumberFormatException nfe) {
8 System.err.println("[" + arg + "] is not an integer"
9 + " and will not be included in the sum.");
10 }
11 }
12 System.out.println("Sum = " + sum);
13 }
14 }
java AddArguments3 1 two 3.0 4
[two] is not an integer and will not be included in the sum.
[3.0] is not an integer and will not be included in the sum.
Sum = 5