java异常流中的意外行为
我编写了以下代码片段:-java异常流中的意外行为,java,exception,Java,Exception,我编写了以下代码片段:- public Collection<?> constructResponse (...........) throws RemoteException { while (keyIterator.hasNext()) { String keyValue = (String) keyIterator.next(); keyString = ne
public Collection<?> constructResponse (...........) throws RemoteException {
while (keyIterator.hasNext())
{
String keyValue = (String) keyIterator.next();
keyString = new StringBuilder(); // since multiple keys will be there in map need to ensure every time keyString and valueString is created
valueString = new StringBuilder();
keyString.append(keyValue + ";" + "name");
List<CustomValuePOJO> customPOJOlist = employeeValuesMap.get(keyValue );
for (CustomValuePOJO customPOJO : customPOJOlist )
{
if (protocol == null || protocol.equals(""))
{
valueString.append(rpNatPOJO.getDcnPort() + ":"+ rpNatPOJO.getProtocol() + ";");
}
else if (customPOJO .getProtocol().equals(protocol))
{
valueString.append(customPOJO .getPort() + ":"+ protocol + ";");
}
else
{ throw new RemoteException("Invalid Argument: Unsupported protocol "+ protocol);
}
}
if (valueString.length() == 0)
{
return generateErrorResponse("No info found");
}
responseMap.put(keyString.toString(), valueString.toString());
}
}
之后,如果它直接上线
throw new RemoteException("Invalid Argument: Unsupported protocol "+ protocol);
追加操作中没有出现错误,并在调试透视图中检查。值成功追加到valueString中
请告诉我遗漏了什么图我应该把它作为一个答案,而不仅仅是一个评论 当代码(在调试器中逐步执行的内容)与编译的类文件(实际运行的类文件)不同步时,可能会发生这种行为。由于调试信息与行号关联,因此类文件中的行可能与您看到的源代码中的行不同
尝试运行一个干净的构建,确保类路径上没有可能导致此问题的重复jar。检查jar文件,可能存在代码不匹配的情况。我见过当代码(正在逐步执行的)与类文件(实际运行的)不同步时会发生这种情况
clean build
并调试您的代码异常也会出现在日志中:)您是在循环中执行此操作的-可能一次迭代会进入elseif,而另一次迭代会出现异常?您可以修改此代码吗?如果是这样的话,在抛出异常之前在其中添加一些调试语句(复制并粘贴If条件,可能输出关于正在进行的循环迭代的信息)。这些信息可能会帮你弄清楚这里发生了什么。
throw new RemoteException("Invalid Argument: Unsupported protocol "+ protocol);