*结构化异常处理 之 终止处理

/******************************************/ // //Description: 结构化异常处理 之 终止处理 // Author: gwy // /******************************************/ #include <iostream> #include <windows.h> using namespace std; ////////////////////////////////////// //自动进入__finally AbnormalTermination() ////////////////////////////////////// int Fun() { DWORD dwTemp = 0; __try { dwTemp = 10; } __finally { dwTemp = 100; if(FALSE == AbnormalTermination()) cout<<"自然进入的__finally"<<endl; } return dwTemp; } ////////////////////////////////////// //return ////////////////////////////////////// int Fun1() { DWORD dwTemp = 0; __try{ dwTemp = 10; return dwTemp; } __finally { dwTemp = 100; //return dwTemp; } dwTemp = 1000; return dwTemp; } ////////////////////////////////////// //goto AbnormalTermination() ////////////////////////////////////// int Fun2() { DWORD dwTemp = 0; __try{ dwTemp = 10; goto RtnValue; } __finally { dwTemp = 100; if(AbnormalTermination()) cout<<"通过return,goto,continue,break等进入"<<endl; } dwTemp = 1000; RtnValue: return dwTemp; } ////////////////////////////////////// //continue break ////////////////////////////////////// int Fun3() { DWORD dwTemp = 0; while(dwTemp<10) { __try{ if(2 == dwTemp) continue; if(3 == dwTemp) break; } __finally { dwTemp++; } } return dwTemp; } ////////////////////////////////////// //__leave ////////////////////////////////////// bool Fun4() { HANDLE hFile = INVALID_HANDLE_VALUE; PVOID pvBuf = NULL; bool bFunctionOK = false; __try { hFile = CreateFile("test.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); if(INVALID_HANDLE_VALUE == hFile) __leave; pvBuf = VirtualAlloc(NULL,1024,MEM_COMMIT,PAGE_READWRITE); if(NULL == pvBuf) __leave; DWORD dwNumBytesRead = 0; bool fOK = true; fOK = ReadFile(hFile,pvBuf,1024,&dwNumBytesRead,NULL); if(!fOK || (0 == dwNumBytesRead)) __leave; cout<<(char*)pvBuf<<endl; bFunctionOK = true; } __finally { if(NULL != pvBuf) VirtualFree(pvBuf,1024,MEM_RELEASE|MEM_DECOMMIT); if(INVALID_HANDLE_VALUE != hFile) CloseHandle(hFile); } return bFunctionOK; } ////////////////////////////////////// //main ////////////////////////////////////// void main(int argc,TCHAR*argv[]) { cout<<Fun()<<endl<<endl; cout<<Fun1()<<endl<<endl; cout<<Fun2()<<endl<<endl; cout<<Fun3()<<endl<<endl; if(Fun4()) cout<<"Read success..."<<endl; else cout<<"Read fail..."<<endl; }

你可能感兴趣的:(*结构化异常处理 之 终止处理)