如果可以方便的添加一下try catch在你的代码里。能用来做什么呢?
Code: |
#include #include #ifdef USE_SAFE # define SAFE_FUNCTION(foo,b) foo \ { \ try{ \ b \ } \ catch(...) \ { \ assert(0 && #foo ); \ } \ } #else # define SAFE_FUNCTION(foo,b) foo \ { \ b\ } #endif SAFE_FUNCTION( int foo(int a,int b) , assert(b!=0 && "hi"); printf("%d\n",a/b); return 0; ) int main() { foo(1,1); foo(1,0); return 0; } |
定义了SAFE_FUNCTION这个宏之后,每次定义一个函数都使用这个宏,那么这些函数都被自动的加上了一个try catch对。可惜对于调试工具来说,错误定位却困难了...
没试过其他的,vc只能跟踪到使用SAFE_FUNCTION定义函数的末尾一行。而且一步就跨越过去了。
目前还没有想到有什么用处。也许同一模块的函数具有相同的出错处理的话,可以使用这个宏。修改catch段的处理方式,甚至再用一个宏
Code: |
#ifdef USE_SAFE # define SAFE_FUNCTION(foo,b,error) foo \ { \ try{ \ b \ } \ catch(...) \ { \ error \ } \ } #else # define SAFE_FUNCTION(foo,b,error) foo \ { \ b\ } #endif #define DEF_SAFE_FUNCTION(foo,b) SAFE_FUNCTION(foo, b, assert(0 && #foo)) #define ANOTHER_SAFE_FUNCTION(foo,b) SAFE_FUNCTION(foo, b, \ printf("Error in %s\n", #foo); \ ) |
这样,在不同模块的处理可以是不同的。
( 网页浏览 )
文章来源:http://acm.tongji.edu.cn/people/kaikai/blog/blog.php?job=art&articleid=a_20041125_142918