First of all, I would just like to say that I know this program could be completedly more easily by using an if-else configuration or a while loop, but I want to know how to do it using the exception class. (I'm guessing I need to use a custom made exception utilizing the try-catch block).
import java.util.Scanner;
class AgeChecker
{
public static void main(String [] args)
{
Scanner inputdata = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = inputdata.nextLine();
System.out.print(name+", enter your age: ");
int age = inputdata.nextInt();
try
{ // age entered must be between 0-125 (how to trigger exception in catch block?)
System.out.println("You entered: "+age);
}
catch(Exception e)
{
System.out.println("Out of range error! (must be between ages 0 and 125)"+e);
}
finally
{
System.out.println("Age Checking Complete.");
}
}
}