免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1859 | 回复: 1
打印 上一主题 下一主题

[C] 请教两个问题:try catch怎么用,信号处理为什么会没有用了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-22 22:21 |只看该作者 |倒序浏览
本帖最后由 leinchu 于 2010-05-22 22:23 编辑

exception.h

  1. #ifndef __EXCEPTION_H__   
  2. #define __EXCEPTION_H__   
  3.    
  4. #include <stdio.h>
  5. #include <setjmp.h>
  6. #include <assert.h>
  7.    
  8.    
  9. #define T Except_t   
  10. typedef struct Except_t{   
  11.         char *reason;   
  12. }Except_t;   
  13.             
  14. typedef struct Except_frame{   
  15.         struct Except_frame *prev;   
  16.         jmp_buf env;   
  17.         const char *file;   
  18.         int line;   
  19.         const T* exception;   
  20. }Except_frame;
  21.    
  22. extern Except_frame *Except_stack;      //全局变量     
  23.    
  24. //异常的状态常量     
  25. enum {EXCEPT_ENTERED=0,EXCEPT_RAISED,   
  26.      EXCEPT_HANDLED,EXCEPT_FINALIZED};     
  27.    
  28. #define throw(e) except_raise(&(e),__FILE__,__LINE__)   
  29.    
  30. #define rethrow except_raise(except_frame.exception,\
  31.                    except_frame.file,except_frame.line)





  32. void abort_without_exception(const Except_t *e,const char *file,int line);   
  33.    
  34. //将栈顶元素从栈中弹出,重新抛出     
  35. void except_raise(const T *e,const char *file,int line);   
  36.                
  37. //try的作用就是将一个包含环境变量env的except_frame压入Except_stack栈中。   
  38. //其中except_flag为setjmp的返回值,表示异常的状态     
  39. #define try do{ \
  40.             volatile int except_flag; \
  41.             Except_frame except_frame; \
  42.             except_frame.prev = Except_stack; \
  43.             Except_stack = &except_frame; \
  44.             except_flag = setjmp(except_frame.env); \
  45.             if (except_flag == EXCEPT_ENTERED) \
  46.             {   
  47.    
  48. //如果和刚刚压入Except_stack中的except_frame对应的longjmp不发生,   
  49. //就将其从栈里面弹出来,而如果发生的话,就恢复这个环境变量所   
  50. //保存的环境,回到setjmp()的位置重新进行处理,这时候except_flag   
  51. //的值为EXCEPT_RAISED     
  52. #define catch(e) \
  53.                  if(except_flag == EXCEPT_ENTERED) \
  54.                      Except_stack = Except_stack->prev; \
  55.              }else if(except_frame.exception == &(e)){ \
  56.                  except_flag = EXCEPT_HANDLED;

  57.    
  58.             #define try_return \
  59.                    switch(Except_stack = Except_stack->prev,0) \
  60.                        default: return     
  61.    
  62.             #define catch_else \
  63.                    if(except_flag == EXCEPT_ENTERED) \
  64.                        Except_stack = Except_stack->prev; \
  65.                        }else{ \
  66.                      except_flag = EXCEPT_HANDLED;   
  67.    
  68. //如果没有相关的处理函数,重新抛出     
  69. #define end_try \
  70.                 if(except_flag == EXCEPT_ENTERED) \
  71.                     Except_stack = Except_stack->prev; \
  72.                 } \
  73.                 if (except_flag == EXCEPT_RAISED) \
  74.                     except_raise(except_frame.exception, \
  75.                         except_frame.file,except_frame.line); \
  76.                 }while(0)   
  77.                     
  78.                     
  79. #define finally \
  80.                 if(except_flag == EXCEPT_ENTERED) \
  81.                     Except_stack = Except_stack->prev; \
  82.                 }{ \
  83.                     if(except_flag == EXCEPT_ENTERED) \
  84.                         except_flag = EXCEPT_FINALIZED;     
  85.    
  86. #undef T   
  87. #endif   
复制代码
exception.c
  1.    
  2. #include "exception.h"
  3. Except_frame *Except_stack = NULL;   
  4.    
  5. void except_raise(const Except_t *e,const char *file,int line)   
  6. {   
  7.      Except_frame *p = Except_stack;   
  8.          
  9.      assert(e);   
  10.      if(p == NULL){   
  11.           abort_without_exception(e,file,line);   
  12.      }   
  13.      p->exception = e;   
  14.      p->file = file;   
  15.      p->line = line;   
  16.      Except_stack = Except_stack->prev;   
  17.      longjmp(p->env,EXCEPT_RAISED);   
  18. }   
  19.    
  20. void abort_without_exception(const Except_t *e,const char *file,int line)   
  21. {   
  22.      fprintf(stderr,"Uncaught exception");   
  23.      if(e->reason)   
  24.          fprintf(stderr," %s",e->reason);   
  25.      else   
  26.          fprintf(stderr," at 0x%p",e);   
  27.          
  28.      if (file && line > 0)   
  29.          fprintf(stderr, "raised at %s:%d\n",file,line);   
  30.      fprintf(stderr,"aborting...\n");   
  31.      fflush(stderr);   
  32.      abort();   
  33. }
复制代码
下面这样用不行.
usage.c

  1. #include "exception.h"
  2. [code]
  3. main()
  4. {
  5.         Except_t exception1;
  6.         try{
  7.                 printf("%s\n",12);
  8.         }
  9.         catch(exception1)
  10.         {
  11.                 printf("%s\n",exception1.reason);
  12.         }end_try;
  13. }

复制代码
2 signal(SIGPIPE, SIG_IGN);

一直用得好好的,由于加了一些东西,不知到什么时候就不管用了,
报下面的错:

#0 0x005b87a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0x00753978 in send () from /lib/tls/libpthread.so.0
#2 0x0804f6a4 in sendMsg ()
#3 0x0804f89e in sendToAll ()
#4 0x08051009 in handle_message ()
#5 0x0804f546 in mainWork ()
#6 0x0804df91 in main ()


谢谢!!!!

非常感谢!

http://notownme.javaeye.com/blog/94540

论坛徽章:
0
2 [报告]
发表于 2010-05-22 22:22 |只看该作者
exception.h里面的代码也很诡异,

}{
或者
大括号都不封闭居然也能编译通过!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP