while (it.hasNext()){
key = it.next();
try{
cc = (ChatClient)chatClients.get(key);
cc.sendln(str);
}catch(Exception e){
//e.printStackTrace();
try {
cc.close();
} catch (Exception e1) {
e1.printStackTrace(); }
chatClients.remove(key);
continue;
}
}---------
运行以上代码,当发生异常时,循环就退出了。已经捕获异常,并处理了,为什么循环不能继续?
*异常是这个方法抛出的: cc.sendln(str) -> writer.write(s);writer是用socket输出流构造的OutputStreamWriter对象;writer = new OutputStreamWriter(socket.getOutputStream());
key = it.next();
try{
cc = (ChatClient)chatClients.get(key);
cc.sendln(str);
}catch(Exception e){
//e.printStackTrace();
try {
cc.close();
} catch (Exception e1) {
e1.printStackTrace(); }
chatClients.remove(key);
continue;
}
}---------
运行以上代码,当发生异常时,循环就退出了。已经捕获异常,并处理了,为什么循环不能继续?
*异常是这个方法抛出的: cc.sendln(str) -> writer.write(s);writer是用socket输出流构造的OutputStreamWriter对象;writer = new OutputStreamWriter(socket.getOutputStream());
解决方案 »
- BufferedReader读取文本 首行多一个‘?’
- 有关重写JPanel中paintComponent方法,解释
- 取字符串的问题,求高手解答
- 谁能帮我用SWT写一个简单的聊天程序!要源代码,功能说明在文章内容里写着呢!很急!
- 高手进来下,帮我解决一个问题,对你们来讲很easy的
- 登录出错!
- 新手请求高手推荐servlet书
- 我当老师的180天(Beta版)/Java部分/第四章
- Java的HTML parser哪一个比较好用?
- 多个系统整合,统一帐户登陆的问题,希望高手能够提供解决方案
- 毕业设计是智能排课系统,要用遗传算法!还要用JSP编!怎么用遗传算法啊???小妹跪求大哥们了!
- java调用ocx控件,提示 Class ID not found in registry result = -2147221005
key = it.next();
try{
cc = (ChatClient)chatClients.get(key);
cc.sendln(str);
}catch(Exception e){
//e.printStackTrace();
continue;
try {
cc.close();
} catch (Exception e1) {
e1.printStackTrace(); }
chatClients.remove(key);
continue;
}
}
这两句不写 chatClients.remove(key);
continue;
试试看
这个也许throw out runtime exception.
具体要调试一下.
有可能是抛出异常后,运行到continue语句,会继续执行循环,
但是it.hasNext()为false,当然就结束循环了啊。
while (true){
if (it.hasNext() == false)
{
System.out.println("it has not next element");
break;
}
key = it.next();
try{
cc = (ChatClient)chatClients.get(key);
cc.sendln(str);
}catch(Exception e){
//e.printStackTrace();
try {
cc.close();
} catch (Exception e1) {
e1.printStackTrace(); }
chatClients.remove(key);
continue;
}
}
这样来检验一下退出循环到底是因为异常还是因为Iterator中已经没有下一个元素了。