This is my first time working with Java exceptions. I have made a program that works to specifications, but I am wondering if there is anything I've done which is unconventional or not recommended. The application program does specify that there be line breaks between each message.
My driver is:
import java.util.Scanner;
public class ReadStrings {
public static void main(String[] args) throws StringTooLongException{
String str1;
final int MAX_STRING_LENGTH = 20;
Scanner input = new Scanner(System.in);
System.out.println("Enter strings, enter DONE when finished:\n");
str1 = input.nextLine();
if(str1.equalsIgnoreCase("done"))
System.exit(0); //exit on first prompt
do{
try
{
if(str1.length()>MAX_STRING_LENGTH)
throw new StringTooLongException();
System.out.println("\nEnter a string, enter DONE when finished:\n");
str1 = input.nextLine();
}
catch(StringTooLongException e)
{
System.out.println(e.getMessage());
str1 = input.nextLine();
}
}while(!str1.equalsIgnoreCase("done"));
input.close();
}
}
And my custom exception class is:
public class StringTooLongException extends Exception {
public StringTooLongException()
{
super("\nString has too many characters\n\nPlease try again:\n");
}
}
Thanks for taking a look. Any advice is appreciated.