简体   繁体   中英

unexpected behavior in java exception flow

I have written following code snippet:-

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());
                }

}

The weird behavior which is happening is that while iterating through the customPOJO its coming inside elseIf and also setting the value in valueString by executing below code:

else if (customPOJO .getProtocol().equals(protocol))
                    {
                        valueString.append(customPOJO .getPort() + ":"+ protocol + ";");
                    } 

After this elseif its coming directly on line

throw new RemoteException("Invalid Argument: Unsupported protocol "+ protocol);

There is no error which is coming in append operation and checked in debug perspective the value is getting appended successfully in valueString.

Please tell what i am missing

Figure I should put this as an answer instead of just a comment...

This sort of behavior can occur when your code (what you are stepping through in the debugger) is out of sync with the compiled class files (that are actually running). Since debug information is associated with line numbers, the lines may be different in the class files than in the source code you see.

Try running a clean build and make sure that there are no duplicate jars on your classpath that may be causing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM