Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 847|回复: 0
打印 上一主题 下一主题

异常处理

[复制链接]

1228

主题

1997

帖子

7582

积分

认证用户组

Rank: 5Rank: 5

积分
7582
跳转到指定楼层
楼主
发表于 2020-1-1 14:58:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 Qter 于 2020-1-28 21:06 编辑

http://www.firemail.wang:8088/forum.php?mod=viewthread&tid=3969
atexit
  1. /*
  2. * Copyright (c) 2009,CVICSE
  3. * All rights reserved.
  4. *
  5. * 文件名称:atexit.cpp
  6. * 文件标识:atexit exit
  7. * 摘 要:atexit的参数类型为void (__cdecl *)(void)
  8. *
  9. * 当前版本:1.0
  10. * 作 者:draeag
  11. * 完成日期:2009年6月22日
  12. *
  13. * 取代版本:
  14. * 原作者 :
  15. * 完成日期:
  16. */




  17. #include <stdio.h>
  18. #include <stdlib.h>

  19. static void atexit_handler_1(void)
  20. {
  21.         printf("within 'atexit_handler_1'\n");
  22. }
  23. static void atexit_handler_2(void)
  24. {
  25.         printf("within 'atexit_handler_2'\n");
  26. }


  27. int main(void)
  28. {
  29.         atexit(atexit_handler_1);
  30.         atexit(atexit_handler_2);
  31.         exit(EXIT_SUCCESS);
  32.         printf("this line should never appear\n");
  33.         return 0;
  34. }
复制代码
  1. /*
  2. * Copyright (c) 2009,CVICSE
  3. * All rights reserved.
  4. *
  5. * 文件名称:except1.cpp
  6. * 文件标识:try catch
  7. * 摘 要:数组越界异常处理
  8. *
  9. * 当前版本:1.0
  10. * 作 者:draeag
  11. * 完成日期:2009年6月22日
  12. *
  13. * 取代版本:
  14. * 原作者 :
  15. * 完成日期:
  16. */

  17. #include <iostream>

  18. using namespace std;

  19. template<class T>
  20. class Array
  21. {
  22.         int i;
  23.         T *ar;
  24. public:
  25.         Array(int c) : i( c )
  26.         {
  27.                 ar = new T[c];
  28.         }
  29.         void init(int n, T x)
  30.         {
  31.                 ar[n] = x;
  32.         }
  33.         T& operator[] (int n)
  34.         {
  35.                 if( n >= i )
  36.                         throw n;
  37.                 return ar[n];
  38.         }
  39. };

  40. void main()
  41. {
  42.         Array<int> array(5);
  43.         cout<<"Please input every element's value:"<<endl;
  44.         try
  45.         {
  46.                 for( int i = 0; i < 5; i++ )
  47.                 {
  48.                         cout<<"No." << i + 1 <<": ";
  49.                         cin>>array[i];
  50.                 }
  51.                 cout << array[5]; //测试数组越界从而导致异常处理的产生
  52.         }
  53.         catch(int x)
  54.         {
  55.                 cout << "A error array ID number has benn found. This value is "<< x <<endl;
  56.         }
  57. }
复制代码
  1. /*
  2. * Copyright (c) 2009,CVICSE
  3. * All rights reserved.
  4. *
  5. * 文件名称:except2.cpp
  6. * 文件标识:异常处理类 服务对象类
  7. * 摘 要:被异常处理扔出的参数也可以为类对象,这种类专门用于处理系统错误的,
  8. *                故称为异常处理类,与之相关联的产生异常的类便被称为服务对象类.两者即可以是同级并行的,
  9. *                也可以作为服务对象类的包容类而存在。前者多用于处理整个系统的各种异常处理;
  10. *                而后者则是专用于为一个系统的异常处理而设置的。
  11. *
  12. * 当前版本:1.0
  13. * 作 者:draeag
  14. * 完成日期:2009年6月22日
  15. *
  16. * 取代版本:
  17. * 原作者 :
  18. * 完成日期:
  19. */

  20. #include <iostream>

  21. using namespace std;

  22. class Err
  23. {
  24.         int err_number;
  25. public:
  26.         Err(int x) : err_number(x)
  27.         {
  28.                 cout << "A error array ID number has been found." <<endl;
  29.         }
  30.         int get_err_number()
  31.         {
  32.                 return err_number;
  33.         }
  34. };

  35. template<class T>
  36. class Array
  37. {
  38.         int i;
  39.         T *ar;
  40. public:
  41.         Array(int c) : i( c )
  42.         {
  43.                 ar = new T[c];
  44.         }
  45.         void init(int n, T x)
  46.         {
  47.                 ar[n] = x;
  48.         }
  49.         T& operator[] (int n)
  50.         {
  51.                 if( n >= i )
  52.                         throw Err(n);
  53.                 return ar[n];
  54.         }
  55. };

  56. void main()
  57. {
  58.         Array<int> array(5);
  59.         cout<<"Please input every element's value:"<<endl;
  60.         try
  61.         {
  62.                 for( int i = 0; i < 5; i++ )
  63.                 {
  64.                         cout<<"No." << i + 1 <<": ";
  65.                         cin>>array[i];
  66.                 }
  67.                 cout << array[5]; //测试数组越界从而导致异常处理的产生
  68.         }
  69.         catch(Err& ex)
  70.         {
  71.                 cout << "This value is "<< ex.get_err_number() <<endl;
  72.         }
  73. }
复制代码
  1. /*
  2. * Copyright (c) 2009,CVICSE
  3. * All rights reserved.
  4. *
  5. * 文件名称:setjmp.cpp
  6. * 文件标识:非局部goto机制
  7. * 摘 要:setjmp(j)设置goto指针,jmp_buf用当前程序上下文信息来初始对象j.
  8. *                这种上下文信息典型包括位置指针、堆栈与框架指针,还有其寄存器与内存值。
  9. *                当初始化上下文件信息后,setjmp返回0。稍后调用longjmp(j, r)的结果,
  10. *                goto到对象j指定的地方(之前调用setjmp进行初始化j的地方),
  11. *                当调用的目标非局部goto时,setjmp返回r,如果r是0返回1.
  12. *
  13. * 当前版本:1.0
  14. * 作 者:draeag
  15. * 完成日期:2009年6月22日
  16. *
  17. * 取代版本:
  18. * 原作者 :
  19. * 完成日期:
  20. */



  21. #include <setjmp.h>
  22. #include <stdio.h>

  23. jmp_buf j;
  24. void raise_exception(void)
  25. {
  26.         printf("exception raised\n");;
  27.         longjmp(j, 8);//jump to exceptino handler  稍后调用longjmp(j, r)的结果,goto到对象j指定(之前调用setjmp进行初始化j)的地方(goto指针)
  28.         printf("this line should never appear\n");
  29. }

  30. int main(void)
  31. {
  32.         if(  setjmp(j) == 0 )   //设置goto指针  jmp_buf用当前程序上下文信息来初始对象j,当初始化上下文件信息后,setjmp返回0
  33.         {
  34.                 printf("'setjmp' is initializing 'j'\n");
  35.                 raise_exception(); //Restore context
  36.                 printf("this line should never appear\n");
  37.         }
  38.         else
  39.         {
  40.                 printf("'setjmp' was just jumped into\n");;//this coide is the exception handler
  41.         }
  42.         return 0;
  43. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-4 02:01 , Processed in 0.054824 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表