Use the exception class MessageTooLongException in a program
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
########### MessageTooLongException.java ###############
public class MessageTooLongException extends Exception{
// default message constant declaration
private final static String message = \"Message Too Long!\";
// default constructor
public MessageTooLongException(){
super(message); // passing to parent class construction so this message can be
// retrieved from getMessage
}
}
################## MessageTooLongExceptionTest.java ################
import java.util.Scanner;
public class MessageTooLongExceptionTest {
public static void main(String[] args) {
// creating Scanner Object
Scanner sc = new Scanner(System.in);
String op = \"y\";
while(op.charAt(0) == \'Y\' || op.charAt(0) == \'y\'){
// getting user input
System.out.println(\"Enter a line of text having no more than 20 characters: \");
String line = sc.nextLine();
// getting length of entered text
int length = line.length();
try{
if(length > 20)
throw new MessageTooLongException();
else
System.out.println(\"You entered \"+length+\" characters, which\"+
\"is an acceptavle length\");
}catch(MessageTooLongException e){
System.out.println(e.getMessage());
}
System.out.println(\"Do you want to another line (Y/N) ? \");
op = sc.next();
}
}
}
/*
Sample run:
Enter a line of text having no more than 20 characters:
THis is a line that is having more than 20 characters
Message Too Long!
Do you want to another line (Y/N) ?
n
*/
############ CoreBreachException.java #############
public class CoreBreachException extends Exception {
// default message constant declaration
private final static String message = \"Core Breach! Evacuate Ship!\";
// default constructor
public CoreBreachException(){
super(message); // passing to parent class construction so this message can be
// retrieved from getMessage
}
}


