Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following custom Exception class has been defined for you: Now write a class

ID: 3775377 • Letter: T

Question

The following custom Exception class has been defined for you:

Now write a class called GetNames that reads in a list of names from the user and then saves each name that is inputted to file named "NameList.txt" using a method called appendToFile() that adds one line of data to the file at a time and has the following signature:

Your GetNames program will use a loop to reads in one full name at a time, not a separate first and last name, and your program will keep reading in names until the word "DONE" is entered by the user. Your program only has two methods, main() and appendToFile(), and all you have to write is the main() method for this program (assume the other method is already written).

However, to write the main() method, you need to know that the appendToFile() method throws an IOException when there is a problem writing to the file. Furthermore, in your loop when you read in a full name that is larger than 30 characters, you must throw the NameTooBig exception. In your main() method you must catch and handle each of these two Exceptions without ending the program, and you must print out an appropriate error message. Remember, you only need to write the main() method, not the appendToFile() method, so you don't need to write any Java File operation commands:

Explanation / Answer

package com.myjava.exceptions;

public class MyOwnException {
public static void main(String[] a){
try{
MyOwnException.myTest(null);
} catch(MyAppException mae){
System.out.println("Inside catch block: "+mae.getMessage());
}
}

static void myTest(String str) throws MyAppException{
if(str == null){
throw new MyAppException("String val is null");
}
}
}

class MyAppException extends Exception {

private String message = null;

public MyAppException() {
super();
}

public MyAppException(String message) {
super(message);
this.message = message;
}

public MyAppException(Throwable cause) {
super(cause);
}

@Override
public String toString() {
return message;
}

@Override
public String getMessage() {
return message;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote