How do I implement proper Exception handling in Java? -
i writing simple program in java in response question. question says :
you fixing bug documents (represented string) failing converted proper xml. problem can occur because either:
a. characters fail in xml, or
b. documents passed in have length > 100 characters (they’re small documents)
it lists 5 characters need escaping (e.g. replace '">'" escape sequence;).
i've coded part replace special characters escapes, i'm not sure length.
if (length of string > 100) { what? }
i thinking of maybe implementing try catch statement, that's used runtime exceptions correct? (null pointer, etc). in design standpoint, best way avoid bug while @ same time completing job of function?
you not handling exception here, rather should throw one:
private static final int max_length = 100; // ... if (inputstring.length() > max_length) { throw new illegalargumentexception( string.format("string large. found %d characters, maximum %d.", inputstring.length(), max_length)); }
this typical way notify user of method they've violated api. make sure method documentation specifies string cannot greater 100 characters.