import java.util.Scanner; import java.util.InputMismatchException; /** * Example of exception handling for Thread.sleep(). Note that * InterruptedException is a checked exception. Any method that could throw this * exception must either catch it or declare that it will throw it. * * @author sam scott */ public class Except1 { /** * Pauses the program based on how many frames per second the user wants. * For example, if they want 20 frames per second, the pause time should be * 1000/20 = 50 ms. * * @param fps Number of frames per second * @throws java.lang.InterruptedException */ public static void pause(int fps) throws InterruptedException { int pauseTime = 1000 / fps; Thread.sleep(pauseTime); System.out.println("Paused for " + pauseTime + " ms."); } /** * TTD: Fix this so that the program compiles. TTD: Deal with the possible * division by 0 exception. * * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("How many frames per second?"); int framesPerSecond = 60; while (true) { try { framesPerSecond = sc.nextInt(); pause(framesPerSecond); break; } catch (InputMismatchException e ) { System.out.println("Error: please supply an integer, please try again"); sc.next(); } catch (InterruptedException e) { System.out.println("Interrupted Exception thrown, please try again"); } catch (IllegalArgumentException e) { System.out.println("Error: must be a positive integer, please try again"); } catch (ArithmeticException e) { System.out.println("Error: integer must be > 0, please try again " + e.getMessage()); } catch (Exception e ) { System.out.println("Error: unknown, please try again "); } } } }