import java.util.Scanner; import java.util.InputMismatchException; public class Lecture { public static void main (String [] args) { try { //throw an exception int [] a = {1,2,3,4}; System.out.println("a[0] = " + a[0]); System.out.println("a[1] = " + a[1]); System.out.println("a[2] = " + a[2]); System.out.println("a[3] = " + a[3]); System.out.println("a[4] = " + a[4]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("caught the exception:\n" + e); System.out.println("the array is shorter..."); } } public static void main2 () { //throw an error System.out.println("another time!"); main2(); } public static void main3 () { try { System.out.println("I'm going to throw my exception"); throw new MyException("MY MESSAGE"); } catch (MyException e) { System.out.println("inside the catch statement"); } } public static void main4() { //Write a program that asks the user to enter an integer //than is ≥ 5 and ≤ 10. The program should continue prompting //the user until a valid number is entered. System.out.println("Please enter a number between 5 and 10. K thanks bye"); Scanner in = new Scanner(System.in); boolean accepted = false; int val = -1; while(!accepted) { //ask for input try { val = in.nextInt(); } catch (InputMismatchException e) { String error = in.nextLine(); System.out.println("Input must be an int, not \"" + error + "\".\n"); System.out.println("MY EXCEPTION!!! = " + e); continue; } //check for if valid if(val < 5) { System.out.println("This integer is less than 5, try again..."); } else if(val > 10) { System.out.println("This integer is greater than 10, try again..."); } else { accepted = true; } } System.out.println("horray i have a value = " + val); } }