// ExceptionalNumbers.java (c) 2004 Kari Laitinen // http://www.naturalprogramming.com // 2004-10-07 File created. // 2005-05-30 Last modification. import java.util.* ; class NumberTooSmallException extends Exception { } class NumberTooLargeException extends Exception { } class ExceptionalNumbers { static int get_number_from_keyboard() throws NumberTooSmallException, NumberTooLargeException { Scanner keyboard = new Scanner( System.in ) ; int number_from_keyboard = keyboard.nextInt() ; if ( number_from_keyboard <= 99 ) { throw new NumberTooSmallException() ; } else if ( number_from_keyboard >= 999 ) { throw new NumberTooLargeException() ; } return number_from_keyboard ; } static int get_number() throws NumberTooLargeException { int number_from_above_method = 1234 ; try { number_from_above_method = get_number_from_keyboard() ; } catch ( NumberTooSmallException number_too_small_exception ) { System.out.print( "\n NumberTooSmallException caught. " ) ; } return number_from_above_method ; } public static void main( String[] not_in_use ) { System.out.print( " Please, type in a number: " ) ; try { int number_read_via_several_methods = get_number() ; System.out.print( "\n The number from keyboard is : " + number_read_via_several_methods ) ; } catch ( NumberTooLargeException number_too_large_exception ) { System.out.print( "\n NumberTooLargeException caught. " ) ; } catch ( Exception caught_exception ) { System.out.print( "\n Some Exception was caught. Some info: " ) ; caught_exception.printStackTrace() ; } } }