/** * @(#)NestedExceptionTest.java * * NestedExceptionTest application * * @author * @version 1.00 2011/1/10 */ import jdg.ch05.KeyboardInput; import java.lang.System; import java.lang.Exception; import java.io.IOException; class VowelException extends Exception{} class ConstantException extends Exception{} class BlankException extends Exception{} class ExitException extends Exception{} class NestedExceptionTest { static KeyboardInput kbd= new KeyboardInput(System.in); public static void main(String args[]) { do{ try{ processUserInput(); }catch(IOException x){ System.out.println("Not acceptable.Try again.\n"); }catch(Exception x){ System.out.println(x); } }while(!exitExceptionTest()); } static boolean exitExceptionTest(){ try{ System.out.println("Exit(y/n): "); System.out.flush(); char ch=Character.toUpperCase(kbd.getChar()); if(ch=='Y') return true; else return false; }catch(IOException iox){ return false; } } static void processUserInput() throws ExitException, Exception{ System.out.print("Enter a character: "); System.out.flush(); char ch; } try{ ch=Character.toUpperCase(kbd.getChar()); }catch(IOException x){ System.out.println("An IOException occured."); return; } switch(ch){ case 'A': case 'E': case 'I': case 'O': case 'U': throw new Exception("You enter a vowel"); case '1': throw new Exception("You enter a number"); case 'X': throw new ExitException(); default: throw new Exception("You enter an invalid Value"); } } }