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 CatchErr3 { /** * OK-ish exception handling. The issue here is that the try covers too much ground * 1. There are two completely different exceptions in two different places * 2. The exception wraps the while loop. Hence, when thrown, the while loop exits. * This may be desired, but may not .. and it should not be done this way if is is desired * @param args Unused. */ public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int prev=10; try { while (prev>=0) { System.out.print("Enter a number: "); String line = br.readLine(); int data = Integer.parseInt(line); System.out.println(data + " / " + prev + " = " + (data / prev)); prev=data; } } catch (IOException ioe){ System.err.println("Problem reading from keyboard" + ioe); } catch(NumberFormatException e) { System.out.println("That's not a number!"); } } }