- 论坛徽章:
- 0
|
本帖最后由 leinchu 于 2010-05-22 22:23 编辑
exception.h
- #ifndef __EXCEPTION_H__
- #define __EXCEPTION_H__
-
- #include <stdio.h>
- #include <setjmp.h>
- #include <assert.h>
-
-
- #define T Except_t
- typedef struct Except_t{
- char *reason;
- }Except_t;
-
- typedef struct Except_frame{
- struct Except_frame *prev;
- jmp_buf env;
- const char *file;
- int line;
- const T* exception;
- }Except_frame;
-
- extern Except_frame *Except_stack; //全局变量
-
- //异常的状态常量
- enum {EXCEPT_ENTERED=0,EXCEPT_RAISED,
- EXCEPT_HANDLED,EXCEPT_FINALIZED};
-
- #define throw(e) except_raise(&(e),__FILE__,__LINE__)
-
- #define rethrow except_raise(except_frame.exception,\
- except_frame.file,except_frame.line)
- void abort_without_exception(const Except_t *e,const char *file,int line);
-
- //将栈顶元素从栈中弹出,重新抛出
- void except_raise(const T *e,const char *file,int line);
-
- //try的作用就是将一个包含环境变量env的except_frame压入Except_stack栈中。
- //其中except_flag为setjmp的返回值,表示异常的状态
- #define try do{ \
- volatile int except_flag; \
- Except_frame except_frame; \
- except_frame.prev = Except_stack; \
- Except_stack = &except_frame; \
- except_flag = setjmp(except_frame.env); \
- if (except_flag == EXCEPT_ENTERED) \
- {
-
- //如果和刚刚压入Except_stack中的except_frame对应的longjmp不发生,
- //就将其从栈里面弹出来,而如果发生的话,就恢复这个环境变量所
- //保存的环境,回到setjmp()的位置重新进行处理,这时候except_flag
- //的值为EXCEPT_RAISED
- #define catch(e) \
- if(except_flag == EXCEPT_ENTERED) \
- Except_stack = Except_stack->prev; \
- }else if(except_frame.exception == &(e)){ \
- except_flag = EXCEPT_HANDLED;
-
- #define try_return \
- switch(Except_stack = Except_stack->prev,0) \
- default: return
-
- #define catch_else \
- if(except_flag == EXCEPT_ENTERED) \
- Except_stack = Except_stack->prev; \
- }else{ \
- except_flag = EXCEPT_HANDLED;
-
- //如果没有相关的处理函数,重新抛出
- #define end_try \
- if(except_flag == EXCEPT_ENTERED) \
- Except_stack = Except_stack->prev; \
- } \
- if (except_flag == EXCEPT_RAISED) \
- except_raise(except_frame.exception, \
- except_frame.file,except_frame.line); \
- }while(0)
-
-
- #define finally \
- if(except_flag == EXCEPT_ENTERED) \
- Except_stack = Except_stack->prev; \
- }{ \
- if(except_flag == EXCEPT_ENTERED) \
- except_flag = EXCEPT_FINALIZED;
-
- #undef T
- #endif
复制代码 exception.c-
- #include "exception.h"
- Except_frame *Except_stack = NULL;
-
- void except_raise(const Except_t *e,const char *file,int line)
- {
- Except_frame *p = Except_stack;
-
- assert(e);
- if(p == NULL){
- abort_without_exception(e,file,line);
- }
- p->exception = e;
- p->file = file;
- p->line = line;
- Except_stack = Except_stack->prev;
- longjmp(p->env,EXCEPT_RAISED);
- }
-
- void abort_without_exception(const Except_t *e,const char *file,int line)
- {
- fprintf(stderr,"Uncaught exception");
- if(e->reason)
- fprintf(stderr," %s",e->reason);
- else
- fprintf(stderr," at 0x%p",e);
-
- if (file && line > 0)
- fprintf(stderr, "raised at %s:%d\n",file,line);
- fprintf(stderr,"aborting...\n");
- fflush(stderr);
- abort();
- }
复制代码 下面这样用不行.
usage.c
- #include "exception.h"
- [code]
- main()
- {
- Except_t exception1;
- try{
- printf("%s\n",12);
- }
- catch(exception1)
- {
- printf("%s\n",exception1.reason);
- }end_try;
- }
复制代码 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 |
|