public class MainClassCutsom {
public static void main(String[] args) throws CustomExceptionClass{
Scanner in = new Scanner(System.in);
System.out.println("Enter the player name");
String playerName = in.nextLine();
System.out.println("Enter the player age");
Integer age = Integer.parseInt(in.nextLine());
CustomExceptionClass c = new CustomExceptionClass(playerName, age);
try {
if(c.getAge() >= 19) {
c.displayDetails();
} else {
throw new CustomExceptionClass("InvalidAgeRangeException");
}
} catch(Exception e) {
System.out.println("CustomException:"+e.getMessage ());
}
in.close();
}
}
I just want to know that the way i constructed this code is a good practice or not. Becaus, when i used only throws and throw keyword the output which i got in the console is in red lines and track race, which i don't want that way to see my output. For that I've used this catch block to print the right message which i got like this below.
Output:
---------
Enter the player name
kumar
Enter the player age
12
CustomException:InvalidAgeRangeException