Class 2 Intermediate Java 30-IT-397  

Comprehensive Example

public class ExceptionExample {

    /** Creates a new instance of ExceptionExample */
    public ExceptionExample() {
 
    }
 
 
    // Add each value of the array.
    public void compute(String[] args) {
        // sum holds our total.
        int sum = 0;
 
        // loop through each array element.
        for (int i = 0; i < args.length; i++) {
 
            // convert the element to a number.
            int number = Integer.parseInt(args[i]);
 
            // add it.
            sum += number;
        }
    }
 
 
    // main method, called from command line.
    public static void main(String args[]) {
        // Make a new ExceptionExample object, call compute.
        ExceptionExample e = new ExceptionExample();
        e.compute(args);
    }

}

java.lang.NumberFormatException: a
        at java.lang.Integer.parseInt(Integer.java:414)
        at java.lang.Integer.parseInt(Integer.java:463)
        at ExceptionExample.compute(ExceptionExample.java:28)
        at ExceptionExample.main(ExceptionExample.java:40)
Exception in thread "main"
   Making Your Own Exception


Created by:  Brandan Jones December 17, 2001