当前位置:编程学习 > JAVA >>

java代码逻辑问题

这是有关异常的一个教学小程序
ErrorCodeType readFile{
initialize errorCode = 0;
open the file;
if(theFileOpen){
determine its size;
if(gotTheFileLength){
allocate that much memory;
if(gotEnoughMemory){
read the file into memory;
if(readFailed){ errorCode = -1; } //1. 读取文件失败,errorCode = -1

}else{ 
errorCode = -2; //2. 分配足够内存失败,errorCode = -2
}
}else{
errorCode = -3; //3. 获取文件大小失败,errorCode = -3
}
close the file;
//4. 如果文件关闭失败,errorCode = -4;
// 否则,errorCode = errorCode and -4;
if(theFileDidntClose && errorCode == 0){
errorCode = -4;
}else{
errorCode = errorCode and -4;
}

}else{
errorCode = -5; //5. 打开文件失败,errorCode = -5
}

return errorCode;
}

问题就在4. ,4的else中“errorCode = errorCode and -4”,通过这个等式,明白它的意思是,else中肯定是文件关闭失败了。
但是else应该还有一种情况是:文件关闭成功但是errorCode != 0;这时不应该“and -4”了,不知道我理解的这个逻辑有问题吗? --------------------编程问答-------------------- 楼主的理解是对的,而且这里还有一种情况:文件关闭成功且errorCode==0。这时候就更不应该"and -4"了。
上面的情况值包括了文件关闭失败且errorCode!=0。
嵌套太多,很容易出错,这种情况最好多看几遍。
希望对你有所帮助! 
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,