栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++翁恺学习32-异常基本概念

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++翁恺学习32-异常基本概念

run-time error
  • the basic philosophy of c++ is that "badly formed code will not be run"
  • there's always something happen in run-time
  • it is very important to deal with all possible situation in the future running
read a file
  • open the file
  • determine its size
  • allocate that much memory
  • read the file into memory
  • close the file
errorCode Type readeFile{
    initialize errorCode = 0;
    open the file;
    if(theFileOpen){
        determine its size;
        if(gotTheFileLength){
            allocate that much memory;
            if(gotEnoughMemoty){
                read the file into memory;
                if(readFailed){
                    errorCode = -1;
                }
            }else{
                errorCode = -2;
            }
        }else{
            errorCode = -3;
        }
        close the file;
        if(theFIEDidntClose&&errorCode ==0){
            errorCode = -4;
        }else{
            errorCode = errorCode and -4;
        }
    }else{
        errorCode = -5;
    }
    return errorCode;
}
working w/exception  
try{
    open the file;
    determine its size;
    allocate that much memory;
    read the file into memory;
    close the file;
}catch(fileOpenFailed){
    doSomting;
}catch(sizeDeterminationFailed){
    doSomting;
}catch(memoryAllocationFailed){
    doSomting;
}catch(readFailed){
    doSomting;
}catch(fileCloseFailed){
    doSomting;
}
exception
  • I take exception to that
  • At the point where the problem occurs,you might not known what to do with it,but you do know that you can't just continue on merrily;you must stop ,and somebody,somewhere,must figure out what to do
why exception? 清晰
  • the significant benefit of exceptions is that they clean up error handing code  
  • it separates the code that describes what you want to do from the code that is executed

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/836112.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号