/** quick-and-sleazy demo of try-catch blocks @author Sharon Tuttle @version 2021-09-15 */ public class TryCatchDemo1 { /** complains if not given at least one argument, and complains if the first argument is not an integer, but if it gets an integer for the argument, it cheers to the screen that many times (and ignores any additional arguments) @param args expected at least one of type int, number of cheers desired */ public static void main(String[] args) { // complain and exit if not at LEAST one command line argument if (args.length < 1) { System.err.println("At least one argument required"); System.exit(1); } // if I get here, have at least one argument! BUT it an int? int givenValue = 0; try { givenValue = Integer.parseInt(args[0]); } catch (NumberFormatException exc) { System.err.println("First arg should be an integer!"); System.exit(1); } catch(Exception exc) { System.out.println("Did I reach here?"); System.out.println(exc.getMessage()); } for (int i=0; i < givenValue; i++) { System.out.println("Yippee!"); } } }