Reputation: 127
I am trying to Deserialize XML file containing methods. Snippet looks like:
XMLDecoder decoder = new XMLDecoder(os);
deSerializedObject = decoder.readObject();
decoder.close();
return deSerializedObject;
When XML file does not contain method for every object.It throws NoSuchMethodException @ readObject.
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)
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)
Problem is I can not catch this exception. What is the proper way to handle it?
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;
Here error is "Unreachable catch block for NoSuchMethodException. This exception is never thrown from the try statement body"
XML Looks like
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0" class="java.beans.XMLDecoder">
<object class="java.util.ArrayList">
<void method="add">
<object class="InfoParameters">
<void property="Name">
<string>Jay</string>
</void>
<void property="Level">
<string>10</string>
</void>
</object>
<object class="InfoParameters">
<void property="Name">
<string>Hir</string>
</void>
<void property="Level">
<string>10</string>
</void>
</object>
</void>
</object>
</java>
Upvotes: 2
Views: 1625
Reputation: 61512
Why can't you catch this exception?
XMLDecoder decoder = new XMLDecoder(os);
try
{
deSerializedObject = decoder.readObject();
}
catch(NoSuchMethodException nsme)
{
System.err.println(nsme.getMessage());
}
catch(Exception e)
{
//try me
}
decoder.close();
return deSerializedObject;
Upvotes: 3
Reputation: 314
If for some reason "catch (Exception e)" is not working try:
try {
...
} catch (Throwable t) {
...
}
This should catch every possible exception and error, since it's the base class for all exceptions.
Upvotes: 0