日期:2014-05-20  浏览次数:20712 次

为何出现这种异常
最近作程序,出现一种异常我不太明白:

while   (!stop)   {


String   str   =   dis.readUTF();//这一行

if   (str   !=   null)   {
......

}


}  

这是客户端读信息的代码,服务端用dos.writeUTF()方法写信息;客户端第一次读信息没有问题,可是第二次执行的时候,一运行到标注的行,就会抛IOException异常,用e.getMessage方法看了一下,居然是空的。查了一下api文档:the   error   message   string   of   this   Throwable   object   if   it   was   created   with   an   error   message   string;   or   null   if   it   was   created   with   no   error   message.我不太明白,既然no   error   message,为什么还会抛异常,还是null。


------解决方案--------------------
readUTF()
如果达到文件结尾,或者不符合UTF编码规则,就会抛出IO异常