import SimpleOutput; /** * A simple test of exceptions. * * @author Samuel A. Rebelsky * @author Carl Caffeinated * @author Carla Caffeinated * @version 1.0 of May 2000 */ public class Exceptional { // +---------+--------------------------------------- // | Methods | // +---------+ /** * Call another method using an array of strings. * If that method throws an exception, we print a * nice little note. If that method succeeds, we * throw an exception. */ public static void antiExceptional(SimpleOutput out, String[] stuff) throws Exception { try { sampleMethod(stuff); throw new Exception("That method succeded"); } catch (Exception e) { out.println("Failed because: " + e.getMessage()); } } // antiExceptional(SimpleOutput, String[]) /** * Look at an array of strings. * @exception Exception * If the array is empty or contains an empty string. */ public static void sampleMethod(String[] stuff) throws Exception { if (stuff == null) { throw new Exception("Null array"); } else if (stuff.length == 0) { throw new Exception("Empty array"); } for (int i = 0; i < stuff.length; i++) { if (stuff[i].equals("")) { throw new Exception("Element " + i + " is empty"); } } // for } // sampleMethod(String[]) // +------+------------------------------------------ // | Main | // +------+ public static void main(String[] args) throws Exception { antiExceptional(new SimpleOutput(), args); } } // class Exceptional