Also available as CmdLine.java

/**
 * Title: CmdLine
 * Description: Demo of custom exception
 * @author hollingd@cs.rpi.edu
 */

// create our own type of exception
// exceptions are just objects !

class CmdLineException extends Exception {
    CmdLineException(){}
    CmdLineException(String s) { super(s); }
}



public class CmdLine{
    int []x = new int[3];

    // Title: ParseArgs
    // Description: Checks the command line args for validity
    // and converts from strings.

    void ParseArgs( String[] args) throws CmdLineException {
        if (args.length<3) {
            throw new CmdLineException("Not enough parameters");
        }
        int i=0;
        try {
            for (i=0;i<3;i++) 
                x[i] = Integer.parseInt(args[i]);
        } catch (NumberFormatException nfe) {
            throw new CmdLineException("Invalid argument format: " + args[i]);
        } catch (Exception e) {
            throw new CmdLineException("Unknown command line error");
        }
    }


    void Print() {
        System.out.print("Args: ");
        for (int i=0;i<x.length;i++) {
            System.out.print(x[i]+ " ");
        }
        // just print newline
        System.out.println();
    }

    public static void main(String[] args) {
        CmdLine c = new CmdLine();

        try {
            c.ParseArgs(args);
            c.Print();
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }
}