import java.util.Scanner; import java.util.NoSuchElementException; import java.util.InputMismatchException; public class ExceptionHandling { public static int readInt(Scanner sc) throws AbnormalSituationException { try { return sc.nextInt(); } catch(NoSuchElementException ex) { throw new AbnormalSituationException("Something bad happened!"); } } public static double getRatio(Scanner sc) throws AbnormalSituationException { System.out.println("Width: "); int width = readInt(sc); System.out.println("Height: "); int height = readInt(sc); return width / (double)height; } public static void main(String []args) { double ratio = -1.0; try (Scanner sc = new java.util.Scanner(System.in)){ ratio = getRatio(sc); System.out.format("Ratio: %.2f", ratio); } catch(AbnormalSituationException ex) { System.out.println(ex); StackTraceElement[] st = ex.getStackTrace(); for(StackTraceElement e: st) System.out.println(e); } } }