Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1252935
  • 博文数量: 160
  • 博客积分: 4132
  • 博客等级: 中校
  • 技术积分: 2086
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 21:56
文章分类

全部博文(160)

文章存档

2012年(25)

2011年(120)

2010年(15)

分类: C/C++

2012-05-24 00:17:29




点击(此处)折叠或打开

  1. #include <iostream.h>
  2. #include <stdlib.h>

  3. //【例15.1】编写一个除法函数Div,要求避免除数为零的情况。
  4. //方法一:按照传统处理除数为零的问题,需先判断除数是否为零,若为零告警出错;非零做除法运算,返回结果。

  5. double Div(double a,double b)
  6. {
  7.  if (b==0) //除数b为零,出错告警中断
  8.  {
  9.   cout<<"Error:attempted to divid by zero!\n";
  10.   exit(1);
  11.  }
  12.  else return a/b; //除数b为非零,正常返回除法运算结果
  13. }
  14. void main15_1_1()
  15. {
  16.  cout<<"8.4/2.1="<<Div(8.4,2.1)<<endl;
  17.  cout<<"8.4/0.0="<<Div(8.4,0.0)<<endl;
  18.  cout<<"8.4/3.1="<<Div(8.4,3.1)<<endl;
  19. }

  20. //方法二:利用C++中提供的异常机制来处理除数为零的问题

  21. double Div1(double a,double b)
  22. {
  23.  if (b==0) throw b; //发现异常,抛出异常对象b
  24.   return a/b;
  25. }

  26. void main15_1_2()
  27. {
  28.  try {
  29.    cout<<"8.4/2.1="<<Div1(8.4,2.1)<<endl;
  30.    cout<<"8.4/0.0="<<Div1(8.4,0.0)<<endl;
  31.    cout<<"8.4/3.1="<<Div1(8.4,3.1)<<endl;
  32.  }
  33.  catch(double) //异常处理程序
  34.  {
  35.   cout<<"Error:attempted to divid by zero!\n";
  36.  }
  37. }

  38. //【例15.2】定义一个Vector类,它可以用异常来处理下标越界的错误。

  39. class Vector
  40. {
  41.  int *p; //p为Vector向量指针
  42.  int sz; //sz为Vector向量元素的大小
  43. public:
  44.  Vector(int size)
  45.  {
  46.   p=new int[size];
  47.   sz=size;
  48.  }
  49.  ~Vector()
  50.  { delete[]p; }
  51.  int size() {return sz;}
  52.  class Range{}; //下标越界异常类
  53.  int& operator[](int i)
  54.  {
  55.   if (i>=0&&i<sz) return p[i];
  56.   throw Range(); //下标越界抛出Range()类型的异常对象
  57.  }
  58. };
  59. //在Vector类内定义了一个空类Range,用它来生成Vector的异常对象表示下标越界。
  60. //在重载下标运算符函数中,当检测到下标运算符越界时,用throw Range()抛出Range
  61. //类型的异常对象。

  62. void f(Vector& v)
  63. {
  64.  try { //检测下标越界异常
  65.        v[0]=1;
  66.       v[v.size()+5]=10; //下标越界的错误
  67.       cout<<"Try block end."<<endl;
  68.  }
  69.  catch(Vector::Range){ //下标越界异常处理
  70.      cout<<"Vector::out of range!"<<endl;
  71.  }
  72.  cout<<"Function f() end."<<endl;
  73. }

  74. void main15_2()
  75. {
  76.  Vector vv(5);
  77.  f(vv);
  78. }

  79. //【例15.3】 自定义一个基类,并从该基类公有派生两个不同派生类,实现对各种不同类型的捕获后的异常处理。

  80. class Base
  81. {
  82. public:
  83.  Base(){ };
  84. };
  85. class Derive1:public Base
  86. {
  87. public:
  88.  int s;
  89.  Derive1(int ss)
  90.  { s=ss; }
  91. };

  92. class Derive2:public Base
  93. {
  94. public:
  95.  Derive2(){ };
  96. };

  97. void fun()
  98. {
  99.  Derive1 Li(0); //定义一个Derive1类的对象Li
  100.  int i,k=6;
  101.  for (i=0;i<=k;i++)
  102.  {
  103.        try{
  104.    switch(i){
  105.     case 0: throw 10; //抛出int型异常
  106.     case 1: throw 10.5; //抛出double型异常
  107.     case 2: throw 'a'; //抛出char型异常
  108.     case 3: throw Li; //抛出Derive1类的Li异常对象
  109.     case 4: throw Derive2(); //抛出Derive2类的异常对象
  110.     case 5: throw "abcd"; //抛出字符串的异常
  111.     case 6: throw Base(); //抛出Base类的异常对象
  112.    }
  113.    cout<<"switch end.\n";
  114.   }
  115.   catch(int) //捕获int型异常后的异常处理
  116.   { cout<<"catch a int.\n";}
  117.   catch(double& value) //捕获double型异常后的异常处理
  118.   { cout<<"catch a double,this value is "<<value<<"\n";}
  119.   catch(char) //捕获char型异常后的异常处理
  120.   { cout<<"catch a char.\n";}
  121.   catch(Derive1) //捕获Derive1类异常对象后的异常处理
  122.   { cout<<"catch a Derive1 class.\n";}
  123.   catch(Base) //捕获Base类异常对象后的异常处理
  124.   { cout<<"catch a Base class.\n";}
  125.   catch(...) //捕获前面未捕获异常的所有异常处理
  126.   { cout<<"Nothing is caught.\n"; }
  127.  }
  128. }

  129. void main15_3()
  130. { fun(); }

  131. //【例15.4】当不能处理抛出对象时,可以显示一条自已定义的出错信息"….error!"

  132. #include <eh.h>

  133. typedef void(*PFV)();

  134. void error_message()
  135. {
  136.  cout<<"....error!"<<endl;
  137. }

  138. void fun3() //设置terminate()要调用的函数error_message()
  139. {
  140.  PFV p=set_terminate(&error_message);
  141.  try {
  142.      throw 'a'; //抛出char类型的异常对象
  143.  }
  144.  catch(int){ //捕获int类型的异常对象
  145.      cout<<"catch a int.\n";
  146.  }
  147.  catch(double){ //捕获double类型的异常对象
  148.      cout<<"catch a double.\n";
  149.  }
  150.  set_terminate(p); //恢复terminate()要调用的系统函数abort
  151. }

  152. void main15_4()
  153. { fun(); }

  154. //【例15.5】在Vector类中进行下标越界、数组尺寸超长异常处理。

  155. class Vector5{
  156.  int *p;
  157.  int sz;
  158.  enum{ max=3000}; //数组的最大尺寸
  159. public:
  160.  Vector5(int size){
  161.   if (size<0||size>max)
  162.   throw Size(); //抛出数组尺寸超长异常对象
  163.   p=new int[size];
  164.   sz=size;
  165.  }
  166.  ~Vector5()
  167.  { delete[]p; }
  168.  int size() const { return sz;}
  169.  class Range{}; //下标越界异常类
  170.  class Size {}; //数组尺寸超长异常类
  171.  int& operator[](int i){
  172.  if (i>=0&&i<sz) return p[i];
  173.      throw Range(); //抛出下标越界异常对象
  174.  }
  175. };

  176. void UseVectors(int size,int range){
  177.  Vector5 vv(size); //定义一个大小为size的数组对象vv
  178.  vv[range]=10; //给数组中指定下标range的元素赋值为10
  179. }

  180. void f(int size,int range){
  181.     try{
  182.   UseVectors(size,range);
  183.   cout<<"Unexception in this time.\n"; //没有异常发生
  184.  }
  185.  catch(Vector5::Range) //捕获数组越界的异常处理
  186.  { cout<<"Vector:: out of range!"<<endl; }
  187.  catch(Vector5::Size) //捕获数组尺寸超长的异常处理
  188.  { cout<<"Vector5::Size error!"<<endl;
  189.      size=size>0?--size:0; //将数组尺寸进行正确的调整,进行size--
  190.      cout<<size<<endl;
  191.      f(size,range); //递归进行调整数组尺寸size
  192.  }
  193.  cout<<"Function f() end."<<endl;
  194. }

  195. void main15_5()
  196. {
  197.  int size,range;
  198.  cout<<"input size,range:";
  199.  cin>>size>>range;
  200.  f(size,range);
  201. }

  202. //【例15.6】对算术运算中出现的异常用异常枚举法处理

  203. enum MatheErr{Overflow,Underflow,Zerodivide};

  204. void fun6()
  205. {
  206.      //…
  207.      try {
  208.       //…
  209.       throw Zerodivide;
  210.   }
  211.      catch(MatheErr fe)
  212.   {
  213.    switch(fe)
  214.    {
  215.     case Overflow: //…
  216.     case Underflow: //…
  217.     case Zerodivide:break; //…
  218.    }
  219.   }
  220.    //…
  221. }

  222. void main15_6()
  223. {
  224.  fun6();
  225. }


阅读(5777) | 评论(0) | 转发(0) |
0

上一篇:C++ Virtual详解

下一篇:没有了

给主人留下些什么吧!~~