当前位置:   article > 正文

异常类型和异常变量的生命周期_因读取超过生命周期的变量而失败

因读取超过生命周期的变量而失败
  1. #include <iostream>
  2. using namespace std;
  3. //传统异常处理
  4. int copyfile(char *from, char * to)
  5. {
  6. if (from==NULL)
  7. {
  8. cout<<""<<endl;
  9. return 1;
  10. }
  11. if (to == NULL)
  12. {
  13. return 2;
  14. }
  15. if (*from == 'a')
  16. {
  17. return 3;
  18. }
  19. while(*from!='\0')
  20. {
  21. *to = *from;
  22. to++;
  23. from++;
  24. }
  25. *to = '\0';
  26. return 0;
  27. }
  28. int main00()
  29. {
  30. char buf1[]="sbcdefghgjkl";
  31. char buf2[1024];
  32. int ret = copyfile(buf1,buf2);
  33. switch (ret)
  34. {
  35. case 1:
  36. cout<<"源出错"<<endl;
  37. break;
  38. case 2:
  39. cout<<"目的出错"<<endl;
  40. break;
  41. case 3:
  42. cout<<"过程出错"<<endl;
  43. break;
  44. default:
  45. cout<<"未知异常"<<endl;
  46. break;
  47. }
  48. printf("buf2:\n",buf2);
  49. system("pause");
  50. return 0;
  51. }
  52. //throw抛异常
  53. void copyfile2(char *from, char * to)
  54. {
  55. if (from==NULL)
  56. {
  57. throw 1;
  58. }
  59. if (to == NULL)
  60. {
  61. throw 2;
  62. }
  63. if (*from == 'a')
  64. {
  65. throw 3;
  66. }
  67. while(*from!='\0')
  68. {
  69. *to = *from;
  70. to++;
  71. from++;
  72. }
  73. *to = '\0';
  74. }
  75. //char*异常
  76. void copyfile3(char *from, char * to)
  77. {
  78. if (from==NULL)
  79. {
  80. throw "源异常";
  81. }
  82. if (to == NULL)
  83. {
  84. throw "目的异常";
  85. }
  86. if (*from == 'a')
  87. {
  88. throw "过程异常";
  89. }
  90. while(*from!='\0')
  91. {
  92. *to = *from;
  93. to++;
  94. from++;
  95. }
  96. *to = '\0';
  97. }
  98. class BadSrc
  99. {};
  100. class Badto{};
  101. class BadPro
  102. {
  103. public:
  104. BadPro()
  105. {
  106. cout<<"BadPro构造函数"<<endl;
  107. }
  108. ~BadPro()
  109. {
  110. cout<<"BadPro析构函数"<<endl;
  111. }
  112. BadPro(const BadPro &obj)
  113. {
  114. cout<<"BadPro拷贝构造函数"<<endl;
  115. }
  116. };
  117. //char*异常
  118. void copyfile4(char *from, char * to)
  119. {
  120. if (from==NULL)
  121. {
  122. throw BadSrc();
  123. }
  124. if (to == NULL)
  125. {
  126. throw Badto();
  127. }
  128. if (*from == 'a')
  129. {
  130. throw BadPro();
  131. }
  132. if (*from == 'b')
  133. {
  134. throw new BadPro();
  135. }
  136. while(*from!='\0')
  137. {
  138. *to = *from;
  139. to++;
  140. from++;
  141. }
  142. *to = '\0';
  143. }
  144. int main()
  145. {
  146. char buf1[]="bbcdefghgjkl";
  147. char buf2[1024];
  148. try
  149. {
  150. copyfile4(buf1,buf2);
  151. }
  152. catch (int e)
  153. {
  154. cout<<"int星异常"<<endl;
  155. }
  156. catch (char* p)
  157. {
  158. cout<<"char*异常:"<<p<<endl;
  159. }
  160. catch (BadSrc e)
  161. {
  162. cout<<"BadSrc异常:"<<endl;
  163. }
  164. catch (Badto e)
  165. {
  166. cout<<"Badto异常:"<<endl;
  167. }
  168. //结论2:使用引用 会使用throw时的对象 调用一次构造函数
  169. catch (BadPro &e)
  170. {
  171. cout<<"BadPro &异常:"<<endl;
  172. }
  173. /* //结论1:如果 接收异常使用异常变量 则调用拷贝构造函数
  174. catch (BadPro e)//是把匿名对象拷贝给e 还是e就是那个匿名对象
  175. {
  176. cout<<"BadPro异常:"<<endl;
  177. }*/
  178. //结论3:使用指针接收异常 必须手动释放内存
  179. //结论4:使用指针接收异常可以同时和元素 引用类型接收异常写在一块 但是引用接收异常不能同时和使用元素写在一块
  180. catch (BadPro *e)
  181. {
  182. delete e;
  183. cout<<"BadPro *异常:"<<endl;
  184. }
  185. catch (...)
  186. {
  187. cout<<"未知异常"<<endl;
  188. }
  189. printf("buf2:\n",buf2);
  190. system("pause");
  191. return 0;
  192. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/589633
推荐阅读