异常处理得与失

上传人:无*** 文档编号:244757769 上传时间:2024-10-05 格式:PPT 页数:40 大小:390.50KB
返回 下载 相关 举报
异常处理得与失_第1页
第1页 / 共40页
异常处理得与失_第2页
第2页 / 共40页
异常处理得与失_第3页
第3页 / 共40页
点击查看更多>>
资源描述
Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,异常处理得与失,张银奎,摘要,异常机制为处理软件执行过程中的“意外”情况提供了一种重要手段,但它是否可以取代传统的错误处理方法呢?,C+,语言规范定义了异常处理,但没有规定如何来实现。操作系统是如何分发异常的,异常处理的开销有多大呢?本演讲以,Windows,操作系统和,Visual C+,编译器为例层层剥茧,由浅入深的回答了以上问题,比较深入的探讨了如何在软件开发中正确的使用异常处理机制,并以实验数据和演示分析了滥用异常处理会导致的严重问题。,在我公司的咨询业务中,C+,异常处理一直是最令软件开发公司迷惑的一个问题。,John Robbins,The popular belief is that exceptions provide a straightforward mechanism for adding reliable error handling to our programs.On the contrary,I see exceptions as a mechanism that may cause more ills than it cures.,EXCEPTION HANDLING:A FALSE SENSE OF SECURITY,by Tom Cargill,目录,异常过程,未处理的异常,异常处理的开销,观点和建议,异常来源,CPU,检测到的执行错误,除,0,,,GP,,无效指令,程序产生,Int,3,throw E,Machine Check Exceptions,总线错误,,ECC,错误,,Cache,错误,异常来源,分类,C+,异常处理(,EH,),C+,标准,关键字:,try,catch,throw,结构化异常处理(,SEH,),Structured Exception Handling,Windows,操作系统的异常机制,具体实现依赖于编译器:,_try,_except,_finally,向量式异常处理(,VEH,),Vectored Exception Handling,Windows XP,引入,对,SEH,的补充,异常处理五阶段,发生,表征,捕捉分发,处理,EXCEPTION_CONTINUE_SEARCH(0),EXCEPTION_EXECUTE_HANDLER,(1),EXCEPTION_CONTINUE_EXECUTION(-1),善后,恢复(,resuming exception,),终止(,terminating exception,),异常处理五阶段,-SEH,int,filter(void,),/*Stage 4*/,int,main(void,),_try,if(,some_error,)/*Stage 1*/,RaiseException,(.);/*Stage 2*/,/*Stage 5 of resuming exception*/,_,except(filter,()/*Stage 3*/,/*Stage 5 of terminating exception*/,return 0;,异常处理五阶段,-EH,int,main(void,),try,if(,some_error,)/*Stage 1*/,thow,E();/*Stage 2*/,carch(E,&)/*Stage 3*/,/*Stage 4*/,/*Stage 5*/,return 0;,主动抛出异常,E e=E();_,CxxThrowException(&e,E_E,XCPT_INFO_ADDR);,throw E();,CxxThrowException,#define CXX_FRAME_MAGIC 0 x19930520,#define CXX_EXCEPTION 0 xe06d7363,VOID,NTAPI,_,CxxThrowException,(,IN PVOID Object,IN PVOID,CxxExceptionType,),ULONG_PTR ExceptionInformation3;,ExceptionInformation0=CXX_FRAME_MAGIC;,ExceptionInformation1=(ULONG_PTR)Object;,ExceptionInformation2=(ULONG_PTR),CxxExceptionType,;,RaiseException,(,CXX_EXCEPTION,EXCEPTION_NONCONTINUABLE,3,ExceptionInformation,);,RaiseException,VOID,NTAPI,RaiseException,(,IN ULONG,ExceptionCode,IN ULONG,ExceptionFlags,IN ULONG,NumberParameters,IN CONST ULONG_PTR *,ExceptionInformation,),EXCEPTION_RECORD,ExceptionRecord,=,ExceptionCode,ExceptionFlags,&EXCEPTION_NONCONTINUABLE,NULL,RaiseException,/,指向本函数的指针,NumberParameters,EXCEPTION_MAXIMUM_PARAMETERS?,EXCEPTION_MAXIMUM_PARAMETERS:,NumberParameters,;,RtlCopyMemory,(,ExceptionRecord.ExceptionInformation,ExceptionInformation,ExceptionRecord.NumberParameters,*,sizeof(ULONG_PTR,),);,RtlRaiseException(&ExceptionRecord,);,ExceptionFlags,0,可恢复继续执行的异常,EXCEPTION_NONCONTINUABLE(1),不可恢复继续执行的异常,如果企图恢复继续执行,则会导致,EXCEPTION_NONCONTINUABLE_EXCEPTION,异常,实例,Args,to Child,0012fe28 0012fb44 00000001 ntdll!NtRaiseException+0 xa,0012fe28 004251b8 e06d7363 ntdll!RtlRaiseException+0 x93,e06d7363 00000001 00000003 kernel32!RaiseException+0 x51,0012ff18 00426680 0012ff80 vcexp!_CxxThrowException+0 x39,直接调用,RaiseException,ChildEBP,RetAddr,Args,to Child,0012fbac 77f5bf94 77f96673 0012fea8 0012fbc4 SharedUserData!SystemCallStub+0 x2(FPO:0,0,0),0012fbb0 77f96673 0012fea8 0012fbc4 00000001 ntdll!NtRaiseException+0 xc(FPO:3,0,0),0012fe98 77e738b2 0012fea8 0012ff10 e0000001 ntdll!RtlRaiseException+0 x93,0012fef8 004010d5 e0000001 00000000,00000000,kernel32!RaiseException+0 x51(FPO:Non-,Fpo,),0012ff80 004016d9 00000001 003715a0 00371618 se!main+0 xc5(CONV:,cdecl,)C:dig,dbgexceptionadvexpsese.cpp,34,0012ffc0 77e8141a 00000000,00000000,7ffdf000 se!mainCRTStartup+0 xe9(CONV:,cdecl,)crt0.c 206,0012fff0 00000000 004015f0 00000000 78746341 kernel32!BaseProcessStart+0 x23(FPO:Non-,Fpo,),进入内核,ZwRaiseException,KiDispatchException,RaiseException,RtlRaiseException,NtRaiseException,ZwRaiseException,NTSYSAPI,NTSTATUS,NTAPI,ZwRaiseException,(,IN PEXCEPTION_RECORD,ExceptionRecord,IN PCONTEXT Context,IN BOOLEAN,SearchFrames,);,KiDispatchException,Windows NT/2000 Native API Reference,by Gary,Nebbett,if(,SearchFrames,),if(,PsGetCurrentProcess,()-,DebugPort,=0,|,KdIsThisAKdTrap(Tf,&Context),if(,KiDebugRoutine,&,KiDebugRoutine(Tf,Reserved,Er,&Context,PreviousMode,FirstChance,)!=0)break;,if(,DbgkForwardException(Tf,DebugEvent,FirstChance,)!=0)return;,if(,valid_user_mode_stack_with_enough_space,),Tf,-,Eip,=,KeUserExceptionDispatcher,;,return;,if(,DbgkForwardException(Tf,DebugEvent,LastChance,)!=0)return;,if(,DbgkForwardException(Tf,ExceptionEvent,LastChance,)!=0)return;,ZwTerminateThread(NtCurrentThread,(),Er,-,ExceptionCode,);,返回用户态,ntdll!RtlpUnlinkHandler+0 xd,vcexp!_UnwindNestedFrames+0 x2c,vcexp!_FrameUnwindToState+0 x16d,vcexp!_InternalCxxFrameHandler+0 x319,vcexp!_InternalCxxFrameHandler+0 xe3,vcexp!_CxxFrameHandler+0 x2c,ntdll!ExecuteHandler2+0 x26,ntdll!ExecuteHandler+0 x24,ntdll!KiUserExceptionDispatcher+0 xe,kernel32!RaiseException+0 x51,vcexp!_CxxThrowException+0 x39,vcexp!vc_throw(void)+0 x4c C:DIGDBGEXCEPTION,advexpvcexp.cpp,27,vcexp!main(void)+0 x1d C:DIGDBGEXCEPTION,advexpvcexp.cpp,44,vcexp!mainCRTStartup(void)+0 xe9 crt0.c 206,ke
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!