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

 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!