Ολοκληρωμένο παράδειγμα εξαιρέσεων

class BadArgException extends Throwable {
    public BadArgException() {}
    public BadArgException(String msg) {
        super(msg);
    }
}

class Test {

    /** Verify that the args table is not empty.
     * @throws BadArgException if the table is empty.
     */
    static void verifyArgs(String args[]) throws BadArgException {
        if (args.length == 0)
            throw new BadArgException("Empty table");
    }

    static public void main(String args[]) {
        int exitCode = 0;

        try {
            int i;

            verifyArgs(args);
            for (i = 0; i < args.length; i++)
                System.out.print(args[i]);
            System.out.println();
        } catch (BadArgException e) {
            System.err.println("Bad argument " + e);
            exitCode = 1;
        } finally {
            System.out.println("Argument processing done");
        }
        System.out.println("Program termination");
        System.exit(exitCode);
    }
}