f1509.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:2k
源码类别:

C#编程

开发平台:

Visual C++

  1. //=====================================
  2. // f1509.cpp
  3. // 异常继承体系
  4. //=====================================
  5. #include"myexception.h"
  6. #include<iostream>
  7. #include<exception>
  8. #include<typeinfo>
  9. using namespace std;
  10. //-------------------------------------
  11. void metalPieseProccess(int);
  12. void metalGroupProccess(int);
  13. void hProccess();
  14. void vProccess();
  15. void performTest();
  16. void calcVSize();
  17. bool tooSmallHSize(){ return false; }
  18. bool performTestFail(){ return false; }
  19. bool tooSmallVSize(){ return false; }
  20. //-------------------------------------
  21. int main(){
  22.   try{
  23.     for(int i=0; i<10; ++i) metalGroupProccess(i);
  24.     // 其他加工
  25.   }catch(MyException& me){
  26.     if(string(me.what())=="HardwareErr")  ; // 停机
  27.     if(string(me.what())=="PerformErr")   ; // 加工件离机,温柔停机
  28.     if(string(me.what())=="OtherErr")     ; // 粗暴停机
  29.     cout<<"MyException: "<<me.what()<<"n";  
  30.   }catch(exception& e){
  31.     cout<<"StandardException: "<<typeid(e).name()<<"n";
  32.     // 停机
  33.   }
  34. }//------------------------------------
  35. void metalGroupProccess(int){
  36.   try{
  37.     for(int i=0; i<10; ++i) metalPieseProccess(i);
  38.   }catch(HSizeErr&){
  39.     cout<<"SizeError.n";
  40.     // 片加工尺寸故障...报废本组金属
  41.   }catch(PerformErr&){
  42.     cout<<"PerformErr.n";
  43.     // 性能测试失败善后处理
  44.     throw;
  45.   }
  46. }//------------------------------------
  47. void metalPieseProccess(int){
  48.   hProccess();
  49.   performTest();
  50.   try{
  51.     vProccess();
  52.     // metalPieseProccess
  53.   }catch(VSizeErr&){
  54.     cout<<"VSizeErrReport.n";
  55.     // 纵向尺寸错误...报废本片金属
  56.   }
  57. }//------------------------------------
  58. void calcVSize(){
  59.   int a=1;
  60.   // ...
  61.   if(tooSmallVSize())throw VSizeErr();
  62.   // int* p = new int[1000000000];
  63.   if(a) throw OtherErr();
  64.   // ...
  65. }//------------------------------------
  66. void vProccess(){
  67.   calcVSize();
  68.   //...
  69. }//------------------------------------
  70. void hProccess(){
  71.   if(tooSmallHSize()) throw HSizeErr();
  72.   //...
  73. }//------------------------------------
  74. void performTest(){
  75.   if(performTestFail()) throw PerformErr();
  76.   //...
  77. }//====================================
  78.