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?
throwtry-catchis used exactly for exceptions that are not of theRuntimeExceptiontype.RuntimeExceptionis thethrowsclause in method declaration.