This week I’m covering exception handling when it comes to user errors, specifically with console input. I’ll be using a Scanner class object to read in an int and will implement a try-catch-finally block to handle exceptions, and print a nicer message out to the console instead of the typical stack trace.
Here’s the program below. Again, this time I’m actually using “pre” tags to put the code in a block, which preserves formatting. Unfortunately I can’t go in and adjust the CSS to change the “pre” class to increase width via “display: inline” or something similar.
Here’s a basic implementation:
/* * Programmer: The Dude * Project Name: ExceptionBasics.java * Description: Basics of Exception Handling * */ // Imports and packages import java.util.Scanner; // for scanner object import java.util.InputMismatchException; // This is the ExceptionType we need in the catch block public class ExceptionBasics { public static void main(String[] args) { // Implementing a try-catch block to handle bad user input // Setting up vars and a conditional to repeat the input prompt if user enters bad input int i = 0; boolean done = false; //for the do-while do{ try { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: \t\t"); i = input.nextInt(); done = true; } catch (InputMismatchException e) { System.out.println("You didn't enter an integer"); } finally // This is a block to put potential cleanup code whether exception occurs or not { // Demo output System.out.println("This is the finally block & executes whether exception occurs or not"); } } while(!done); System.out.println("You entered: " + i); } }
So that is a very basic demo of exception handling. Below is a slightly more in-depth demo that uses two catch blocks to handle two types of possible user errors. One is the InputMismatchException imported from the java.util package, and the other is a programmed class called IntegerOutOfBoundsException which extends Exception. Here’s the simple class declaration for that.
// This is the class that we need to catch the // 2nd type of exception, any number entered // which is outside the given range. public class IntegerOutOfRangeException extends Exception {}
And here’s the complete implementation.
/* * Programmer: The Dude * Project Name: CustomExceptionExample.java * Description: Using throws and try-catch-catch example * */ // Imports and packages import java.util.Scanner; import java.util.InputMismatchException; public class CustomExceptionExample { public static void main(String[] args) { /*This example will throw a InputMismatchException when you enter a non-integer*/ //Variable declarations and objects int i; Scanner input = new Scanner(System.in); System.out.print("Enter a number between 1 and 10: "); // Try-catch for 2 exception types. try{ i = input.nextInt(); if(i 10)throw new IntegerOutOfRangeException(); } catch(InputMismatchException ex) { System.out.println("You did not enter an integer"); System.out.println(ex); } catch(IntegerOutOfRangeException ex) { System.out.println("Your value is not in the specified range!"); System.out.println(ex); } } }