0

I am currently writing a simple program in Java in response to a question. The question says :

You are fixing a bug where documents (represented as a String) are failing to be converted to proper xml. This problem can occur because either:
A. Certain characters fail in the xml, or
B. Documents passed in have a length > 100 characters (they’re small documents)

It then lists 5 characters that need escaping (e.g. replace '">'" with the escape sequence;).

I've coded the part to replace special characters with escapes, but I'm not sure what to do about the length.

if (length of string > 100) {
    do what?
}

I was thinking of maybe implementing a try catch statement, but that's used for runtime exceptions correct? (null pointer, etc). In a design standpoint, what would be the best way to avoid this bug while at the same time completing the job of the function?

4
  • Maybe you are thinking of throwing the exception, not catching it --> throw Commented Apr 25, 2013 at 10:53
  • try-catch is used exactly for exceptions that are not of the RuntimeException type. Commented Apr 25, 2013 at 10:53
  • 3
    @TheodorosChatzigiannakis Categorically not true. Commented Apr 25, 2013 at 10:54
  • 2
    @TheodorosChatzigiannakis what is used for exceptions that are not RuntimeException is the throws clause in method declaration. Commented Apr 25, 2013 at 10:58

4 Answers 4

3

You are not handling an exception here, rather you should probably throw one:

private static final int MAX_LENGTH = 100;
// ...

if (inputString.length() > MAX_LENGTH) {
  throw new IllegalArgumentException(
      String.format("String is too large. Found %d characters, maximum is %d.",
          inputString.length(), MAX_LENGTH));
}

This is the typical way you notify a user of your method that they've violated the API. Make sure your method documentation specifies that the String cannot be greater than 100 characters.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what I was looking for, I forgot about throwing exceptions. Thanks.
FWIW that exception message would be much more useful if it included the observed length of the string, and the limit. E.g. "String is too large (found 532 characters, maximum allowed is 100 characters)". Speaking from experience, an error message that just says "no" is less helpful(/more infuriating) than one that tells you why.
@AndrzejDoyle Very true. I omitted that level of detail for brevity, but you are completely correct. I've edited accordingly.
1

If I understand you correctly, your function converts Strings of a length of < 100 to XML. So from a design-perspective I see no problem in throwing a IllegalArgumentException when the given Strings length is >= 100.

You should not try to continue execution, there is a reason why Exceptions stop the execution of the function - to guarantee correct results that fit the contract of the function.

Comments

1

Exceptions should occur when something exceptional occurs. You should never code using an exception to control your business logic (especially if it's a third party exception like a NoEntityFound, what if the rules around the third party exception being thrown change?).

If the length is genuinely something you need to halt for and tell the user about, throw a custom exception such as...

throw new IllegalStringLength("String length invalid");

However, if this is something you can do without throwing an exception and handle more gracefully, I'd generally say it's better to do so. It's not like a null pointer or a database couldn't connect.

You'd then have to catch the above exception and output it to the user (again, using the exception to control your application flow).

Comments

0

Create your own Exception class :

public class FileTooLongForXmlException extends Exception{
    public FileTooLongForXmlException() {
        super("Your file is > 100 carac");
    }
}

And then you just do :

if (length of string > 100) {
    throw new FileTooLongForXmlException();
}

2 Comments

... maybe. I'd always try and use an existing exception if possible (like IllegalArgumentException).
Well, i think it depends, if it's library-oriented it might be usefull to create your own Exceptions, but i agree both answers are okay

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.