我们以三种对代码不一样的处理方式来看一看异常的优点:
没有处理错误的程序:
read-file {
openTheFile;
determine its size;
allocate that much memory;
closeTheFile;
}
以常规方法处理错误
openFiles;
if (theFilesOpen) {
determine the lenth of the file;
if (gotTheFileLength){
allocate that much memory;
if (gotEnoughMemory) {
read the file into memory;
if (readFailed) errorCode=-1;
else errorCode=1;
}else errorCode=-3;
}else errorCode=-5 ;
}else errorCode=-5;