gpt4 book ai didi

java - 如何捕获 NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-04 05:54:43 26 4
gpt4 key购买 nike

我正在尝试反序列化包含方法的 XML 文件。
片段看起来像:

 XMLDecoder decoder = new XMLDecoder(os); 
deSerializedObject = decoder.readObject();
decoder.close();
return deSerializedObject;

当 XML 文件不包含每个对象的方法时。它抛出 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)



问题是我无法捕捉到这个异常。处理它的正确方法是什么?
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;

这里的错误是“无法访问 NoSuchMethodException 的 catch 块。这个异常永远不会从 try 语句体中抛出”

XML 看起来像
<?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>

最佳答案

为什么你不能捕捉到这个异常?

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;

关于java - 如何捕获 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9649151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com