import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * More issues with exception handling * * @author gtowell Created: Feb 11, 2020 */ public class CatchErr4 { /** * better exception handling. The issue here is that the try still covers too * much ground 1. There are two completely different exceptions in two different * places by inner try 2. If dividebyzero can never get out of it! * * @param args Unused. */ public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int prev = 10; while (prev >= 0) { try { System.out.print("Enter a number: "); String line = br.readLine(); try { int data = Integer.parseInt(line); System.out.println(data + " / " + prev + " = " + (data / prev)); prev = data; } catch (NumberFormatException e) { System.err.println("That's not a number!"); } catch (ArithmeticException ae) { System.err.println("Division by Zero" + ae); } } catch (IOException ioe) { System.err.println("Problem reading from keyboard" + ioe); } } } }