TransactionSynchronizationManager的ThreadLocal机制与事务回调实战解析

核心内容摘要

Java SpringBoot+Vue3+MyBatis 学生宿舍管理系统系统源码|前后端分离+MySQL数据库
MAA Assistant Arknights:5大核心价值重新定义明日方舟游戏辅助工具

基于深度学习YOLOv12的葡萄叶病害识别检测系统(YOLOv12+YOLO数据集+UI界面+登录注册界面+Python项目源码+模型)

在C中我们可以使用try/catch来捕获异常但是C语言中没有这样的关键字只有setjmp和longjmp函数它们的常规用法是先设置信号捕获函数再调用setjmp在信号捕获函数中调用longjmp如果出现异常则会跳到setjmp后面#includesetjmp.h#includesignal.h#includestdio.hjmp_buf bufvoidsig(ints){switch(s){caseSIGSEGV:caseSIGINT:caseSIGFPE:(void)signal(s,sig);longjmp(buf,

;break;default:break;}}intmain(intargc,char*argv[]){(void)signal(SIGINT,sig);(void)signal(SIGFPE,sig);(void)signal(SIGSEGV,sig);if(setjmp(buf)

{int*pnullptr;*p0;}else{printf(exception\n);}我们可以使用宏来模拟try,catch#defineTRYif(setjmp(buf)

{#defineCATCH\}\else这样就可以写成下面的形式了TRY{}CATCH{}目前这种形式不支持嵌套捕获比如TRY{TRY{}CATCH{}//如果这里出现异常就不能捕获}CATCH{}要支持捕获也很简单把jmp_buf设置成数组再使用一个变量来记录深度使用一次TRY则加1CATCH时减1即可jmp_buf buf[256]{};uint8_tdepth0;#defineTRYif(setjmp(buf[depth])

{#defineCATCH\--depth;\}\elsevoidsig(ints){switch(s){caseSIGSEGV:caseSIGINT:caseSIGFPE:(void)signal(s,sig);longjmp(buf[--depth],

;break;default:break;}}测试代码#includesetjmp.h#includesignal.h#includestdint.h#includestdio.hjmp_buf buf[256]{};uint16_tdepth0;#defineTRYif(setjmp(buf[depth])

{#defineCATCH\--depth;\}\elsevoidsegv(){int*vnullptr;*v1;printf(%p\n,v);}voiddiv0(){intv0;printf(%d\n,1/v);}voidsig(ints){switch(s){caseSIGSEGV:caseSIGINT:caseSIGFPE:(void)signal(s,sig);longjmp(buf[--depth],

;break;default:break;}}voidtest(){TRY{TRY{segv();}CATCH{printf(SIGSEGV exception\n);}div0();}CATCH{printf(SIGFPE exception\n);}}intmain(intargc,char*argv[]){(void)signal(SIGINT,sig);(void)signal(SIGFPE,sig);(void)signal(SIGSEGV,sig);TRY{test();}CATCH{printf(test exception\n);}printf(ok\n);return0;}如果是使用的VC编译器和调试器在除0的地方会直接断下来并不会继续执行所以触发不了SIGFPE信号这是由于SEH异常机制导致如果想要实现与GCC一样的行为即POSIX行为则需要拦截SEH手动触发SIGFPE信号#if_MSC_VER#includewindows.hLONG WINAPIseh_handler(EXCEPTION_POINTERS*e){if(e-ExceptionRecord-ExceptionCodeEXCEPTION_INT_DIVIDE_BY_ZERO){raise(SIGFPE);returnEXCEPTION_CONTINUE_EXECUTION;}returnEXCEPTION_CONTINUE_SEARCH;}#endif再调用#if_MSC_VERAddVectoredExceptionHandler(1,seh_handler);#endif完整代码#includesetjmp.h#includesignal.h#includestdint.h#includestdio.h#if_MSC_VER#includewindows.hLONG WINAPIseh_handler(EXCEPTION_POINTERS*e){if(e-ExceptionRecord-ExceptionCodeEXCEPTION_INT_DIVIDE_BY_ZERO){raise(SIGFPE);returnEXCEPTION_CONTINUE_EXECUTION;}returnEXCEPTION_CONTINUE_SEARCH;}#endifjmp_buf buf[256]{};uint16_tdepth0;#defineTRYif(setjmp(buf[depth])

{#defineCATCH\--depth;\}\elsevoidsegv(){int*v0;*v1;printf(%p\n,v);}voiddiv0(){intv0;printf(%d\n,1/v);}voidsig(ints){switch(s){caseSIGSEGV:caseSIGINT:caseSIGFPE:(void)signal(s,sig);longjmp(buf[--depth],

;break;default:break;}}voidtest(){TRY{TRY{segv();}CATCH{printf(SIGSEGV exception\n);}div0();}CATCH{printf(SIGFPE exception\n);}}intmain(intargc,char*argv[]){#if_MSC_VERAddVectoredExceptionHandler(1,seh_handler);#endif(void)signal(SIGINT,sig);(void)signal(SIGFPE,sig);(void)signal(SIGSEGV,sig);TRY{TRY{TRY{segv();}CATCH{printf(SIGSEGV exception\n);}div0();}CATCH{printf(SIGFPE exception\n);}test();}CATCH{printf(test exception\n);}printf(ok\n);return0;}如果对你有帮助欢迎点赞收藏

17c最新ip地址复制-17c最新ip地址复制应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123