使用宏简单的模拟try catch

使用宏来模拟try catch,功能比较简单,在vc2008下编译并运行

#include "stdafx.h" #define EX_CATCH_ANY EX_CATCH_ANY #define EX_TRY do{int i_try_flag=1; #define EX_DO(exp,except) if(!exp) {i_try_flag=0; goto CATCH_##except;} #define EX_CATCH(except) while(i_try_flag--){goto FINALLY;} CATCH_##except: i_try_flag=1; #define EX_FINALLY }while(0);FINALLY: int _tmain(int argc, _TCHAR* argv[]) { EX_TRY { int i =0; i++; EX_DO(1,3); EX_DO(0,EX_CATCH_ANY); } EX_CATCH(3) { int i =0; i++; } EX_CATCH(EX_CATCH_ANY) { int i =0; i++; } EX_FINALLY { int i =0; i++; } return 0; }

 

你可能感兴趣的:(c/c++)