Catch it yourself – The Daily WTF

We’ve all seen the blank catch block, “swallow mistakes and ignore them”. David sends us a “top” example of that anti-pattern, with a few bonuses that make it even better.


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 method description here”. Then we accept the so-called parameter bufXMLbut it is a StringBuffer, not an actual XML object, which sets us up for an ugly, obnoxious, “please don’t” string-corrupting approach to interacting with XML. Class name of bean object is used as a tag, we follow the last index of that tag, and then we insert more string-generated XMLs at that position.

Everything about it is bad and horrible. Working with XML is not Nice, but using real XML classes in Java makes it easier and safer. I guess we can say, “at least they’re not regular expressions”.

But the real star of the show is exception handling. We catch any exceptions generated by this string warping. Then we try to… throw a new exception. Which we catch, only to do nothing with it. This codex is like a lonely kid on the playground who throws a ball in the air and only catches it because no one else will play with him.

It would be sad, if it weren’t so terrible.

[Advertisement]

Continuously monitor your servers for configuration changes and report when configuration drift occurs. Get started with Otter today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *