ExampleEleven.cpp
上传用户:skywee
上传日期:2021-12-09
资源大小:141k
文件大小:1k
- #include <stdio.h>
- #include <excpt.h>
- #include <windows.h>
- #include <eh.h>
- class CSE{
- public:
- static void MapSE2CE()
- {
- _set_se_translator(TranslateSE2CE);
- }
- operator DWORD()
- {
- return m_er.ExceptionCode;
- }
- private:
- CSE(PEXCEPTION_POINTERS pep)
- {
- m_er=*pep->ExceptionRecord;
- m_context=*pep->ContextRecord;
- }
- static void _cdecl TranslateSE2CE(UINT dwEC,PEXCEPTION_POINTERS pep)
- {
- throw CSE(pep);
- }
- private:
- EXCEPTION_RECORD m_er;
- CONTEXT m_context;
- };
- int main(int argc, char* argv[])
- {
- //注意,应该在每个线程的入口点调用该静态成员函数。
- CSE::MapSE2CE();
- try
- {
- int *pi=NULL;
- *pi=0;
- int x=0;
- x=5/x;
- }
- catch(CSE se)
- {
- switch(se)
- {
- case EXCEPTION_ACCESS_VIOLATION:
- //...
- break;
- case EXCEPTION_INT_DIVIDE_BY_ZERO:
- //...
- break;
- default:
- throw;
- break;
- }
- }
- return 0;
- }