CodeSOD: Catch By Yourself

This post was originally published on this site

The Daily WTF

We’ve all seen the empty catch block, the “swallow errors and ignore them”. David sends us a “superior” example of that anti-pattern, with several bonuses to make it even more of a WTF.

/** * Insert the method’s description here. * @param bean java.lang.Object * @param bean_template java.lang.StringBuffer * @param bufXML java.lang.StringBuffer */ protected static void appendBeanTemplate(Object bean, StringBuffer bufXML) { int iEndTag; StringBuffer xmlTemplate = new StringBuffer(); try { iEndTag = bufXML.toString().lastIndexOf(“</” + getClassName(bean) + “>”); writeChildBeanDescription(bean, xmlTemplate); bufXML.insert(iEndTag, xmlTemplate.toString()); } catch (Exception e) { try { throw new Exception(“Error creating xml bean description.”); } catch (Exception ex) { } } }

Taking it from the top, we start with an incomplete JavaDoc comment. “Insert the method’s description here”. Then we accept a parameter called bufXML, but it’s a StringBuffer, not an actual XML object, which sets us up for the ugly, hideous, “please don’t” string mangling approach

To read the full article click on the 'post' link at the top.