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);
    }
}

Comments »

The URI to TrackBack this entry is: http://eradicus.blogsome.com/2009/03/15/recovering-from-a-checked-exception-in-java/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator