Digital Stronghold




March 15, 2009

Recovering from a checked exception in Java

If you are working on the back-end, this might be of use. Very trivial but rarely used.

/**
 * This class demonstrates how to recover from checked exceptions
 * @author Joset
 */
public class CheckedExceptionRecovery {
	
    /**
     * @param args the command line arguments
     */
    public static void main(String... args) {
	
        InputStreamReader inputStreamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
	
        int input = 0;
        boolean done = false;
	
        do {
            try {
                System.out.println("Please enter an integer: ");
                input = Integer.parseInt(bufferedReader.readLine().trim());
                done = true;
            } catch (NumberFormatException numberFormatException) {
                System.out.println("Invalid input. Please try again.");
            } catch (IOException ioException) {
                System.out.println("Cannot proceed.");
            }
        } while (!done);
	
        System.out.println("The integer is: " + input);
    }
}

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator