/* */ import javax.swing.*; public class ExceptionMaterial { public static void main(String[] args) { readDate1( ); readDate2( ); // generateException( ); System.exit(0); } public static void generateException( ) { int[ ] a = new int[20]; System.out.println( "We got this far..." ); a[32] = 5; // we're askin' for trouble! System.out.println( "...but we never got here." ); } public static int getYear( String d ) { String yearString = d.substring( 6, 10 ); return Integer.parseInt( yearString ); } public static void readDate1( ) { try { String d = JOptionPane.showInputDialog( "Enter date: (mm/dd/yyyy)"); int year = getYear( d ); System.out.println( "The year is " + year ); } catch ( Exception e ) { System.out.println( "An exception occurred:\n" + e.getMessage( ) ); } } public static void readDate2( ) { String d = ""; try { d = JOptionPane.showInputDialog( "Enter date: (mm/dd/yyyy)"); int year = getYear( d ); System.out.println( "The year is " + year ); } catch ( IndexOutOfBoundsException e ) { System.out.println( "Index error" ); } catch ( NumberFormatException e ) { System.out.println( "Number format exception" ); } finally { System.out.println( "The original string: " + d ); } } }