// ExceptionsDemo1 import java.util.ArrayList; public class ExceptionsDemo1 { public static void main(String[] args) { int numerator = 4; int denominator = 0; System.out.println(numerator/denominator); // ArithmeticException - you can't divide an int by zero int[] scores = {100, 82, 93, 75}; scores[3] = 99; // no problem, the 75 is overwritten with the value 99 in index position 3 scores[10] = 99; // ArrayIndexOutOfBoundsException - there is no index position 10 scores[4] = 99; // ArrayIndexOutOfBoundsException - there is no index position 4 // even though the length of the array is 4 ArrayList numbers = new ArrayList(); // an ArrayList of Integer objects numbers.add(new Integer(19610)); // adding an Integer object with the value 19610 (Wyomissing's zip code) // to numbers, an ArrayList of Integer objects numbers.add(19611); // taking advantage of Java "autoboxing" the int value 19611 (West Reading's // zip code) as an Integer object // String wyoZipCode = ((String) numbers).get(0); // ClassCastException - you can't cast an Integer to a String String westReadingZipCode = numbers.get(0) + ""; // you can concatenate an empty string to an autounboxed int value though System.out.println(westReadingZipCode); ArrayList words = new ArrayList(); words.add("Wyomissing"); words.add("Reading"); words.add("Kutztown"); System.out.println(words.get(1)); // displays Reading which is in index position 1 of the ArrayList words System.out.println(words.get(13)); // IndexOutOfBoundsException - the ArrayList doesn't have an index position number 13 System.out.println(words.get(3)); // IndexOutOfBoundsException - the ArrayList doesn't even have an index position number 3 // even though the size of the ArrayList is 3 // IndexOutOfBoundsException is practically the same as ArrayIndexOutOfBoundsException except it occurs with ArrayLists instead of arrays String name1 = ""; // name1 is declared as an object variable and it has been instantiated as an actual object System.out.println(name1.length()); // the length of name1 is 0 String name2; // name2 is declared as an object variable but it is not instantiated as an actual object // System.out.println(name2.length()); // NullPointerException - name2 hasn't been instantiated yet so it doesn't really exist & has no length String name3 = null; // name3 is declared as an object variable but it is not instantiated as an actual object System.out.println(name3.length()); // NullPointerException - name3 hasn't been instantiated yet so it doesn't really exist & has no length int[] nums1 = new int[5]; // nums1 is an array variable that is declared and instantiated with a length of 5 nums1[2] = 5; // nums1 is now {0, 0, 5, 0, 0} int[] nums2; // nums2 is declared as an array variable but it is not instantiated as an actual object // nums2[2] = 5; // NullPointerException - nums2 hasn't been instantiated yet so it doesn't really exist String animal1 = "cat"; System.out.println(animal1.substring(1, 2)); // displays "a" String animal2 = "dog"; System.out.println(animal2.substring(2, 3)); // displays "g" since we start at the character in index position 2 and extract // everything up to but not including index position 3 String animal3 = "owl"; System.out.println(animal3.substring(15, 16)); // StringIndexOutOfBoundsException - clearly an error since there is no index // position 15 in "owl" String animal4 = "cow"; System.out.println(animal4.substring(3, 4)); // StringIndexOutOfBoundsException - there is no index position 3 in "cow" System.out.println(computeSalesTax(-10)); // IllegalArgumentException - the method "throws" an IllegalArgumentException // since it's logically impossible to have a negative price. This is rarely covered on the AP exam. // IllegalStateException - example will may be demonstrated in class. This is rarely covered on the AP exam. } public static double computeSalesTax(double basePrice) { if (basePrice <= 0) { throw new IllegalArgumentException("The base price must be greater than zero."); } return basePrice * 0.06; } }