title: How to catch NoSuchMethodException
body: <p>I am trying to  Deserialize XML file containing methods.
  Snippet looks like:</p>
  
  <pre><code> XMLDecoder decoder = new XMLDecoder(os); 
      deSerializedObject = decoder.readObject(); 
      decoder.close();
      return deSerializedObject;
  </code></pre>
  
  <p>When XML file does not contain method for every object.It throws NoSuchMethodException @ readObject.</p>
  
  <blockquote>
    <p>java.lang.NoSuchMethodException: No method with name add is found
         at java.beans.Statement.findMethod(Statement.java:454)
        at java.beans.Statement.invokeMethodImpl(Statement.java:247)
        at java.beans.Statement.access$000(Statement.java:46)
        at java.beans.Statement$1.run(Statement.java:130) at java.security.AccessController.doPrivileged(AccessController.java:284)
        at java.beans.Statement.invokeMethod(Statement.java:127)
        at java.beans.Expression.getValue(Expression.java:67)    at java.beans.XMLDecoder$SAXHandler.executeCommon(XMLDecoder.java:392)
        at java.beans.XMLDecoder$SAXHandler.execute(XMLDecoder.java:321)
        at java.beans.XMLDecoder$SAXHandler.endElement(XMLDecoder.java:286)   at
    org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown
    Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
    Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
    Source)
         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)   at
    org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at
    org.apache.xerces.parsers.XMLParser.parse(Unknown Source)  at
    org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)<br>
    at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
    Source)  at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(Unknown Source)  at java.beans.XMLDecoder.readObject(XMLDecoder.java:662)</p>
  </blockquote>
  
  <p>Problem is I can not catch this exception. What is the proper way to handle it?</p>
  
  <pre><code>try{
          deSerializedObject = decoder.readObject();
          System.out.println("We are after read object");
          }
          catch (NoSuchMethodException e)
          {
              System.out.println("We are in catch block !!");
              e.printStackTrace();
          }
          decoder.close();
          return deSerializedObject;
  </code></pre>
  
  <p>Here error is "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body" </p>
  
  <p>XML Looks like </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;java version="1.6.0" class="java.beans.XMLDecoder"&gt;
   &lt;object class="java.util.ArrayList"&gt;
    &lt;void method="add"&gt;
     &lt;object class="InfoParameters"&gt;
      &lt;void property="Name"&gt;
       &lt;string&gt;Jay&lt;/string&gt;
      &lt;/void&gt;
       &lt;void property="Level"&gt;
       &lt;string&gt;10&lt;/string&gt;
      &lt;/void&gt;
     &lt;/object&gt;
     &lt;object class="InfoParameters"&gt;
      &lt;void property="Name"&gt;
       &lt;string&gt;Hir&lt;/string&gt;
      &lt;/void&gt;
      &lt;void property="Level"&gt;
       &lt;string&gt;10&lt;/string&gt;
      &lt;/void&gt;
     &lt;/object&gt;
    &lt;/void&gt;
   &lt;/object&gt;
  &lt;/java&gt;
  </code></pre>