Correct the following code and add appropriate exception handling. To handle exceptions in an elegant manner, ask
Question:
Correct the following code and add appropriate exception handling. To handle exceptions in an elegant manner, ask the user to re-enter data until he enters data in the correct format. Also, add a final block that shows the message as follows:
• If try completes successfully, Finally prints "The program has been completed successfully"
• Finally runs after catch block, prints Finally prints " Repeating input from start again"
public class NewClass {
public static void main(String[] args){
Scanner scan =new Scanner(System.in);
int month = Integer.parseInt(JOptionPane.showInputDialog("Enter month as integer"));
int day = Integer.parseInt(JOptionPane.showInputDialog("Enter day as integer"));
int year =Integer.parseInt(JOptionPane.showInputDialog("Enter year as integer"));
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date aDate = new Date(dDay+"/"+dMonth+"/"+dYear);
System.out.println(dateFormat.format(aDate));
} }
(c):- Create a custom checked exception named MyNameException that will be thrown if the user input is not equivalent to your name.