// // TwoCatchesDemo.java // // P.687, Listing 9.8 // import java.util.Scanner; public class TwoCatchesDemo { public static double exceptionalDivision(double nume, double deno) throws DividedByZeroException { if(deno == 0) throw new DividedByZeroException(); return nume/deno; } public static void main(String[] args) { Scanner kb = new Scanner(System.in); try { System.out.print("Enter # of widgets: "); int widgets = kb.nextInt(); if(widgets < 0) throw new NegativeNumberException("widgets"); System.out.print("How many are defective: "); int defective = kb.nextInt(); if(defective < 0) throw new NegativeNumberException("defectvie widgets"); double ratio = exceptionalDivision(widgets, defective); System.out.println("One in every " + ratio + " + widgets is defective.\n\n\n"); } catch(NegativeNumberException e) { System.out.println("Can't have a negative number of " + e.getMessage()); } catch(DividedByZeroException e) //Note: not include e.getMessage() { System.out.println("Cong! A perfect record!"); } System.out.println("End of program.\n\n\n"); } }