In: Computer Science
EXCEPTION HANDLING
Concept Summary:
1. Exception handling
2. Class design
Description
Write a program that creates an exception class called ManyCharactersException, designed to be thrown when a string is discovered that has too many characters in it.
Consider a program that takes in the last names of users. The driver class of the program reads the names (strings) from the user until the user enters "DONE". If a name is entered that has more than 20 characters, throw the exception.
Design the program such that it catches and handles the exception if it is thrown. Handle the exception by printing an appropriate message, and then continue processing more names by getting more inputs from the user. Note: Your program should exit only when the user enters "DONE".
SAMPLE OUTPUT:
Enter strings. When finished enter DONE
Short string
You entered: Short string
Medium size string
You entered: Medium size string
Really really long string, someone stop me!
You entered too many characters
DONE
You entered: DONE
Good bye!
//Create a class for exception handle name with ManyCharactersException.java
//Create a driver class with name ExceptionDriver.java to check the functionality of ManyCharactersException
//run the driver class
-------------------------------------------------------------------------------------------------------------------------------------------------
ManyCharactersException.java
public class ManyCharactersException extends Exception {
int condition = 0;
public ManyCharactersException(int conditionViolated)
{
super("Invalid Password: ");
condition = conditionViolated;
}
public String printMessage()
{
// Call constructor of parent Exception
// according to the condition violated
switch (condition) {
// designed to be thrown when a string is discovered that has too
many characters in it.
case 1:
return ("You entered: Short String");
// characters near about 20
case 2:
return ("You entered: Medium String");
// characters has more than 20 characters
case 3:
return ("You entered too many characters");
}
return ("You entered: DONE");
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
ExceptionDriver.java code
import java.util.Scanner;
public class ExceptionDriver {
public static void checkString(String name) throws
ManyCharactersException {
//here are the conditions about length of String
//you can change the legth of string for comparision
if (name.equals("DONE")) {
throw new ManyCharactersException(4);
} else if (name.length() <= 12) {
throw new ManyCharactersException(1);//to short
} else if (name.length() > 12 && name.length() < 20)
{
throw new ManyCharactersException(2);
} else if (name.length() > 20) {
throw new ManyCharactersException(3);
}
}
public static void main(String[] args) {
System.out.println("Enter strings. When finished enter
DONE");
Scanner sc = new Scanner(System.in);
String input = "";
while (!input.equals("DONE")) {
try {
input = sc.nextLine();
checkString(input);
} catch (ManyCharactersException ee) {
System.out.println(ee.printMessage());
}
}
System.out.println("Good bye!");
}
}
--------------------------------------------------------------------OUTPUT--------------------------------------------------------------