HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

Custom exception for string that is too long

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
exceptionlongtoocustomthatforstring

Problem

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.

Solution

if(str1.equalsIgnoreCase("done"))
        System.exit(0); //exit on first startup


Do not use System.exit() (somewhere else then in a catch block in main) It terminates the JVM immediately. This is usually not intended.

just write a return instead.

if(str1.length()>MAX_STRING_LENGTH)
                throw new StringTooLongException();
            System.out.println("\nEnter a string, enter DONE when finished:\n");
            str1 = input.nextLine();


This will result in an endless loop when the user enters a long string. The check is done (and the exception thrown) before she gets a chance to enter a new one.

You should move the if behind the input request and delete the initial input request (along wit the if there) before the loop.

Since you do nothing else in the loop the checking the string and requesting new one some may argue that you use the Exception as flow control for which exceptions are not supposed to be used.

I'd agree to that argument in this particular case as long as the "happy path" also repeats the loop...

Code Snippets

if(str1.equalsIgnoreCase("done"))
        System.exit(0); //exit on first startup
if(str1.length()>MAX_STRING_LENGTH)
                throw new StringTooLongException();
            System.out.println("\nEnter a string, enter DONE when finished:\n");
            str1 = input.nextLine();

Context

StackExchange Code Review Q#148026, answer score: 4

Revisions (0)

No revisions yet.