捕获由于无效指针导致的内存读写异常,最后导致进程异常中止(仅适用于Windows系统)

int filter(unsigned int code, struct _EXCEPTION_POINTERS *ep) {
	if (code == EXCEPTION_ACCESS_VIOLATION) {
		return EXCEPTION_EXECUTE_HANDLER;
	}
	else {
		return EXCEPTION_CONTINUE_SEARCH;
	};
}

void CTestNullPointerDlg::OnBnClickedButton1()
{
	int* p = 0x00000000;   // pointer to NULL

	__try{
		__try{
			*p = 13;    // causes an access violation exception;
		}__finally{
			//代码会先跳到这里			
			TRACE(L"in finally");
		}

	}__except(filter(GetExceptionCode(), GetExceptionInformation())){
		//再跳到这里
		TRACE(L"in exception");
		//外面有while的话,在这里也可以使用break;跳出!
	}

	AfxMessageBox(L"success catch null pointer exception!");
}

你可能感兴趣的:(捕获由于无效指针导致的内存读写异常,最后导致进程异常中止(仅适用于Windows系统))