Validating that a Java program has two equal-length arguments I'm writing a small almost one method Java class for a job application and the advice given was to write it as though it was a piece of commercial software.

The processResult method needs two string arguments of equal length. I thought the best thing to do is check that the args has at least 2 items, then inside the processResult method check that the strings have the same length. 

I am unsure about throwing a generic exception in my main method and then later using the try/catch statement. Is this a good practice to do? Would it be better practice to handle these as part of one try catch? Are custom Exceptions a good way to go for this type of problem?

Here is what I have at the moment. 

public class Task {
    public static void main(String[] args) throws Exception {
        if (args.length &lt; 2) {
            throw new Exception("Need 2 string arguments");
        }

        try {
            int result = processResult(args[0], args[1]);
            System.out.println(result);
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }
    }

    private static boolean lengthsEqual(String a, String b) {
        return Integer.compare(a.length(), b.length()) == 0;
    }

    private static int processResult(String inputOne, String inputTwo) {
        if (!lengthsEqual(inputOne, inputTwo) {
            throw new IllegalArgumentException("Strings must be the same length");
        }

        // method logic

        return 0;
    }

}
I took your code and rewrote it a bit, so that it satisfies my personal requirements for production-ready code.

if (args.length &lt; 2) {
    System.err.println("usage: Task &lt;arg1&gt; &lt;arg2&gt;");
    System.exit(1);
}


I removed the throw new Exception since passing the wrong number of arguments is not a programming error but a wrong invocation of the program. Only programming errors should print a stack trace. Instead of saying "Need 2 string arguments" I am following the "usage:" pattern that has been successfully established by Unix programs. Since you didn't provide any context in your question, the best variable names I could come up with are &lt;arg1&gt; &lt;arg2&gt;. If your task were to copy a file, the usage line should better be usage: Copy &lt;source&gt; &lt;target&gt;, of course.

} catch (IllegalArgumentException e) {
    System.err.println(e.getMessage());
    System.exit(1);
}


I added the System.exit since it was missing. If the program fails, it must report this via System.exit or by throwing an exception from the main function.

private static int processResult(String inputOne, String inputTwo) {
    if (inputOne.length() != inputTwo.length()) {
        throw new IllegalArgumentException("Strings must be the same length");
    }


There's no need to have a separate function for checking the lengths of the strings. Using IntelliJ it was quite simple to inline the method call to lengthsEqual (I just pressed Ctrl+Alt+N) and to remove the redundant call to Integer.compare (which was already marked in dark gray, so I just had to press Alt+Enter there).

It's unfortunate that the exception message says "String must be the same length". This message does not tell which strings are meant, and since that message is printed to System.err later, it should be worded with the same care as the usage message.

I disagree with bag's answer, especially the part that commercial code needs to be extensible. It doesn't. It needs to be easy to read, and it needs to clearly tell its intention. If the business requires that the two strings be equal (for whatever reason), the code should say exactly this, and the code should match the wording of the original requirement as closely as possible, so that later changes to the requirements can be programmed as easily as possible.