《c++大学教程》读书笔记 (第13章 异常处理)


      这一章所介绍的似乎简略了一些。当然,我也不知该怎么样写才算说得清楚,毕竟我才初学C++,只是看得我不太爽,有些地方不明白。

这章总的有两点不足:1.说得不够深入。2.例子不够精辟。

      其中有个例子(因为内存比较大,做了点修改)我在上机的时候就发生错误,abort了。一开始就new失败。(VC6.0)


#include <iostream>
#include 
<new>
using namespace std;


void customNewHandler()
{
    cerr
<<customNewHandler was called     abort();
}


int main()
{
    
double *ptr[9999    set_new_handler(customNewHandler);

    

        
for(int i=0<9999++)
     
{
            ptr[i]
=new double[5000000
            cout
<<Allocated 5000000 double s in ptr[<<i<<]n        }

    



    
return 0
}

       跟踪一下,发现在set_new_handler(customNewHandler)就出错了。但用VC7和gcc编译后运行正常,结果均如下:
Allocated 5000000 double s in ptr[0]
Allocated 5000000 double s in ptr[1]
Allocated 5000000 double s in ptr[2]
Allocated 5000000 double s in ptr[3]
Allocated 5000000 double s in ptr[4]
Allocated 5000000 double s in ptr[5]
Allocated 5000000 double s in ptr[6]
Allocated 5000000 double s in ptr[7]
Allocated 5000000 double s in ptr[8]
Allocated 5000000 double s in ptr[9]
Allocated 5000000 double s in ptr[10]
Allocated 5000000 double s in ptr[11]
Allocated 5000000 double s in ptr[12]
Allocated 5000000 double s in ptr[13]
Allocated 5000000 double s in ptr[14]
Allocated 5000000 double s in ptr[15]
Allocated 5000000 double s in ptr[16]
Allocated 5000000 double s in ptr[17]
Allocated 5000000 double s in ptr[18]
Allocated 5000000 double s in ptr[19]
Allocated 5000000 double s in ptr[20]
Allocated 5000000 double s in ptr[21]
Allocated 5000000 double s in ptr[22]
Allocated 5000000 double s in ptr[23]
Allocated 5000000 double s in ptr[24]
Allocated 5000000 double s in ptr[25]
Allocated 5000000 double s in ptr[26]
Allocated 5000000 double s in ptr[27]
customNewHandler was called

     另一个例子是测试new故障抛出bad_alloc异常。


#include <iostream>
#include 
<new>
using namespace std;


int main()
{
    
double *ptr[9999

    
try
    
{
        
for(int i=0<9999++)
        
{
            ptr[i]
=new double[5000000
            cout
<<Allocated 5000000 double s in ptr[<<i<<]n        }

    }

    
catch(bad_alloc &ex)
    
{
        cout
<<Exception occurred:
            
<<ex.what()<<endl;
    }



    
return 0
}



       在VC6.0编译运行时,不知说它聪明还是傻,竟然分配到9998个数组,用了系统虚拟内存。所以看不到抛出异常的结果。

而在gcc和VC7上都得到满意答案。gcc结果如下:

Allocated 5000000 double s in ptr[0]
Allocated 5000000 double s in ptr[1]
Allocated 5000000 double s in ptr[2]
Allocated 5000000 double s in ptr[3]
Allocated 5000000 double s in ptr[4]
Allocated 5000000 double s in ptr[5]
Allocated 5000000 double s in ptr[6]
Allocated 5000000 double s in ptr[7]
Allocated 5000000 double s in ptr[8]
Allocated 5000000 double s in ptr[9]
Allocated 5000000 double s in ptr[10]
Allocated 5000000 double s in ptr[11]
Allocated 5000000 double s in ptr[12]
Allocated 5000000 double s in ptr[13]
Allocated 5000000 double s in ptr[14]
Allocated 5000000 double s in ptr[15]
Allocated 5000000 double s in ptr[16]
Allocated 5000000 double s in ptr[17]
Allocated 5000000 double s in ptr[18]
Allocated 5000000 double s in ptr[19]
Allocated 5000000 double s in ptr[20]
Allocated 5000000 double s in ptr[21]
Allocated 5000000 double s in ptr[22]
Allocated 5000000 double s in ptr[23]
Allocated 5000000 double s in ptr[24]
Allocated 5000000 double s in ptr[25]
Allocated 5000000 double s in ptr[26]
Allocated 5000000 double s in ptr[27]
Exception occurred:St9bad_alloc


  VC7结果如下:
Allocated 5000000 double s in ptr[0]
Allocated 5000000 double s in ptr[1]
Allocated 5000000 double s in ptr[2]
Allocated 5000000 double s in ptr[3]
Allocated 5000000 double s in ptr[4]
Allocated 5000000 double s in ptr[5]
Allocated 5000000 double s in ptr[6]
Allocated 5000000 double s in ptr[7]
Allocated 5000000 double s in ptr[8]
Allocated 5000000 double s in ptr[9]
Allocated 5000000 double s in ptr[10]
Allocated 5000000 double s in ptr[11]
Allocated 5000000 double s in ptr[12]
Allocated 5000000 double s in ptr[13]
Allocated 5000000 double s in ptr[14]
Allocated 5000000 double s in ptr[15]
Allocated 5000000 double s in ptr[16]
Allocated 5000000 double s in ptr[17]
Allocated 5000000 double s in ptr[18]
Allocated 5000000 double s in ptr[19]
Allocated 5000000 double s in ptr[20]
Allocated 5000000 double s in ptr[21]
Allocated 5000000 double s in ptr[22]
Allocated 5000000 double s in ptr[23]
Allocated 5000000 double s in ptr[24]
Allocated 5000000 double s in ptr[25]
Allocated 5000000 double s in ptr[26]
Allocated 5000000 double s in ptr[27]
Exception occurred:bad allocation



     一些基本概念
异常可以处理的情况:
1。内存不足以满足new请求
2。数组下标越界。
3。运算溢出
4。除数为0
5。无效函数参数

不可以处理情况:
1。磁盘I/O完成
2。网络消息送达
3。鼠标单击
4。等等。。。

C++异常处理关键字: try、throw、catch

简单处理例子:除数为0



此条目发表在c++, 技术, 未分类分类目录,贴了标签。将固定链接加入收藏夹。
0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
最旧
最新 最多投票