// StackTrace2 Example // Show how an ArrayIndexOutOfBoundsException // displays an error message and the stack // trace showing all of the method calls on // the system stack when the exception occurs. public class StackTrace2 { public static int[ ] a = {1, 2, 3, 4, 5}; public static void main(String[ ] args) { try { printResult( ); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("User Message:"); System.out.println("Array index out of bounds."); System.out.println( ); System.out.println("Official Diagnosis:"); System.out.println(e); System.out.println( ); System.out.println("Stack Trace:"); e.printStackTrace( ); System.out.println( ); System.out.println("Print detail message string:"); System.out.println(e.getMessage( )); System.out.println( ); } } public static void printResult( ) { int x = getValue(5); System.out.println(x); } public static int getValue(int v) { return a[v]; } }