// file: demoExceptions.java // author: Robert Keller // purpose: Demonstrate a variety of exceptions by throwing them intentionally. import java.io.*; import java.util.*; class demoExceptions { static int numberOfExceptionsDemonstrated = 10; public static void main(String arg[]) { for( int number = 1; number <= numberOfExceptionsDemonstrated; number++) try { // Dispatch on the argument. switch( number ) { case 1: demoArithmeticException(); break; case 2: demoNumberFormatException(); break; case 3: demoNullPointerException(); break; case 4: demoClassCastException(); break; case 5: demoStringIndexOutOfBoundsException(); break; case 6: demoArrayIndexOutOfBoundsException(); break; case 7: demoNegativeArraySizeException(); break; case 8: demoArrayStoreException(); break; case 9: demoNoSuchElementException(); break; case 10: demoFileNotFoundException(); break; } } catch( Exception e ) { System.out.println("*** case " + number + " caught: " + e); } } /** * An ArithmeticException occurs when there is a divide-by-zero error. */ static void demoArithmeticException() /* RuntimeException not declared */ { message("Demonstrate ArithmeticException"); int x = 1/0; // invalid, divide b 0 } /** * A NumberFormatException occurs when an attempt is made to convert * a String to a number, but the String can't be converted. */ static void demoNumberFormatException() /* RuntimeException not declared */ { message("Demonstrate NumberFormatException"); float f; f = Float.parseFloat("-1.23e-34"); // ok f = Float.parseFloat("foobar"); // invalid, can't convert } /** * A NullPointerException occurs when an attempt is made to apply a * method using a null reference. */ static void demoNullPointerException() /* RuntimeException not declared */ { message("Demonstrate NullPointerException"); String s = null; int len = s.length(); // invalid, null reference } /** * A ClassCastException occurs when an attempt is made * to convert an Object to a class to which it doesn't belong. */ static void demoClassCastException() /* RuntimeException not declared */ { message("Demonstrate ClassCastException"); String s = "foo"; Object x = s; Integer i = (Integer)x; // invalid } /** * A StringIndexOutOfBoundsException occurs when an attempt is made * to access a non-existent character in a String. */ static void demoStringIndexOutOfBoundsException() { message("Demonstrate StringIndexOutOfBoundsException"); String s = "Goodbye, cruel world"; char c = s.charAt(100); // invalid index } /** * An ArrayIndexOutOfBoundsException occurs when an attempt is made * to access a non-existent element of an array. */ static void demoArrayIndexOutOfBoundsException() /* RuntimeException */ { message("Demonstrate ArrayIndexOutOfBoundsException"); String a[] = new String[10]; int len = a[10].length(); // invalid index } /** * A NegativeArraySizeException occurs when an attempt is made * to create an array with size < 0. */ static void demoNegativeArraySizeException() /* RuntimeException */ { message("Demonstrate NegativeArraySizeException"); Object x[] = new Object[-1]; // invalid index } /** * An ArrayStoreException occurs when an attempt is made * to store the wrong kind of object into an array of objects. * Note that this is not a ClassCastException because no casting * is involved. */ static void demoArrayStoreException() /* RuntimeException */ { message("Demonstrate ArrayStoreException"); Object x[] = new String[10]; x[0] = new Integer(99); // invalid, store Integer into String array } /** * A NoSuchElementException occurs when an attempt is made * get a non-existent element of an Enumeration. */ static void demoNoSuchElementException() /* RuntimeException */ { message("Demonstrate NoSuchElementException"); StringTokenizer tokenizer = new StringTokenizer("That's all folks"); while( tokenizer.hasMoreElements() ) { tokenizer.nextElement(); // justified tokenizer.nextElement(); // unjustified } } /** * An FileNotFoundException indicates a file named in a command * doesn't exist. */ static void demoFileNotFoundException() throws FileNotFoundException { message("Demonstrate FileNotFoundException"); String badFileName = "This file doesn't exist."; FileInputStream input; input = new FileInputStream(badFileName); } /** * Print a commentary message. */ static void message(String message) { System.out.println("\n" + message); } }