HttpServer 中的 for 循环行为不当
我在 for 循环中遇到了这个奇怪的问题。 该循环仅迭代一次,而应该迭代更多次(其中有 3-4 个元素,具体取决于不相关的元素。
代码如下:
System.out.println("Executing " + url);
System.out.println("_elements.size()=" + _elements.size());
for (Object el : _elements) {
System.out.println("element class:" + el.getClass().getName());
if (el instanceof DynamicServlet) {
((DynamicServlet) el).execute();
_stringBuff.append(((DynamicServlet) el).getOutput());
}
if (el instanceof String)
_stringBuff.append((String)el);
} // for
System.out.println("finished for loop");
程序打印以下行:
Executing /admin.dsp
_elements.size()=4
element class:java.lang.String
请注意,它永远不会打印最后一个 System.out.println!它神秘地中断并退出。 没有抛出异常或任何异常,并且此代码部分由 com.sun.net.httpserver.HttpHandler 的“handle(HttpExchange)”方法执行。
有谁知道这里发生了什么事吗?
提前致谢!
I have this weird problem with a for loop.
This loop only iterates once while it should iterate more times (it has 3-4 elements in it, depending on something irrelevant.
Here is the code:
System.out.println("Executing " + url);
System.out.println("_elements.size()=" + _elements.size());
for (Object el : _elements) {
System.out.println("element class:" + el.getClass().getName());
if (el instanceof DynamicServlet) {
((DynamicServlet) el).execute();
_stringBuff.append(((DynamicServlet) el).getOutput());
}
if (el instanceof String)
_stringBuff.append((String)el);
} // for
System.out.println("finished for loop");
The program prints the following lines:
Executing /admin.dsp
_elements.size()=4
element class:java.lang.String
Notice that it never gets to print the last System.out.println! It mysteriously breaks and exits.
There is no Exception thrown or anything, and this code section is being by the "handle(HttpExchange)" method of com.sun.net.httpserver.HttpHandler.
Does anyone have any idea what's going on here?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您知道没有抛出任何异常吗?或者只是没有报告异常?我会将迭代器块的内容包装在
try/catch
中并报告任何异常。Do you know that no exception is being thrown ? Or simply that no exception is being reported ? I would wrap the contents of the iterator block in a
try/catch
and report any exceptions.