import java.util.Scanner;
import java.util.InputMismatchException;

/**
 * A class to sum up numbers provided by the user.
 *
 * @author Mark Young (A00000000)
 */
public class CatchSumException {

    public static final Scanner KBD = new Scanner(System.in);

    public static void main(String[] args) {
        int num, sum;

        // introduce yourself
        System.out.println("Enter some positive integers below "
                + "and I'll tell you their sum.");

        // add up the user input
        try {
            sum = 0;
            num = KBD.nextInt();
            while (num >= 0) {
                sum += num;
                System.out.println("The sum is now " + sum + ".");
                num = KBD.nextInt();
            }
        
            // report result
            System.out.println("Their sum is " + sum + ".");
        } catch (InputMismatchException ime) {
            System.out.println(KBD.next() + " is not an integer.");
            System.out.println("Your program would have crashed "
                    + "if I hadn't caught that exception!");
        }
    }

}

