// Page 231-232 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 input = new Scanner(System.in); try { System.out.print("Enter # of widgets: "); int widgets = input.nextInt(); if(widgets < 0) throw new NegativeNumberException("widgets"); System.out.print("How many are defective: "); int defective = input.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) { System.out.println("Cong! A perfect record!"); } System.out.println("End of program.\n\n\n"); } }