try catch无法捕获异常设置

try catch无法捕获异常设置

try catch无法捕获异常设置_第1张图片

项目属性修改 

try catch无法捕获异常设置_第2张图片

 

测试代码

    try
    {
		BYTE *pch;
		pch = (BYTE *)01234; //非法地址 
		*pch = 5;
	}
	catch (const std::exception& e)
	{
		printf("msg:%s\n",e.what());
	}
	catch (...)
	{
		//打印堆栈信息
		PrintfStackInfo();
	}

打印堆栈信息代码

#pragma once
#include 
#include 
#include 
#pragma comment(lib,"Dbghelp.lib")

/***********************************
1.PrintfStackInfo(),可打印当前的调用堆栈信息
2.如try catch,无法捕获异常,如除0,访问非法地址等
项目属性中设置 C/C++  ->代码生成  ->启用c++异常选择“是,但有 SEH 异常 (/EHa)”
try
{
	.....
}
catch (...)
{
	PrintfStackInfo();
}
3.如多个函数嵌套,PrintfStackInfo只会打印当前函数的位置

/***********************************/
void wchar2strstring(std::string & szDst, WCHAR * wchart)
{
	wchar_t * wtext = wchart;
	DWORD dwNmu = WideCharToMultiByte(CP_OEMCP, NULL, wtext, -1, NULL, 0, NULL, FALSE);
	char * psTest;
	psTest = new char[dwNmu];
	WideCharToMultiByte(CP_OEMCP, NULL, wtext, -1, psTest, dwNmu, NULL, FALSE);
	szDst = psTest;
	delete[]psTest;
}
void ShowTraceStack(std::vector& vInfo)
{
	vInfo.clear();
	enum
	{
		MAX_STACK_FRAMES = 16
	};

	void* arrBackTrace[MAX_STACK_FRAMES] = { 0 };

	HANDLE hProcess = GetCurrentProcess();
	SymInitialize(hProcess, NULL, TRUE);
	typedef USHORT(*PFUNRtlCaptureStackBackTrace)(
		ULONG  FramesToSkip,
		ULONG  FramesToCapture,
		PVOID* BackTrace,
		PULONG BackTraceHash
		);
	HMODULE hNt = LoadLibrary(_T("NtDll.dll"));
	PFUNRtlCaptureStackBackTrace
		pCaptureStackBackTrace = (PFUNRtlCaptureStackBackTrace)
		GetProcAddress(hNt, ("RtlCaptureStackBackTrace"));
	if (!pCaptureStackBackTrace)
	{
		vInfo.push_back("RtlCaptureStackBackTrace load failed");
		return;
	}
	CString strStackInfo;
	WORD wFrames = pCaptureStackBackTrace(0, MAX_STACK_FRAMES, arrBackTrace, NULL);

	TCHAR szBuffer[sizeof(SYMBOL_INFOW) + MAX_SYM_NAME * sizeof(TCHAR)];
	DWORD64 dwAddress = 0;
	DWORD64 dwDisplacement = 0;
	PSYMBOL_INFOW pSymbol = NULL;
	DWORD dwDisplacement2 = 0;
	IMAGEHLP_LINEW64  ilLine;
	for (WORD wIndex = 0; wIndex < wFrames; ++wIndex)
	{
		dwAddress = (DWORD64)(arrBackTrace[wIndex]);
		dwDisplacement = 0;
		pSymbol = (PSYMBOL_INFOW)szBuffer;
		pSymbol->SizeOfStruct = sizeof(SYMBOL_INFOW);
		pSymbol->MaxNameLen = MAX_SYM_NAME;

		dwDisplacement2 = 0;
		ilLine.SizeOfStruct = sizeof(IMAGEHLP_LINEW64);

		if (TRUE == SymFromAddrW(hProcess, dwAddress, &dwDisplacement, pSymbol) &&
			TRUE == SymGetLineFromAddrW64(hProcess, dwAddress, &dwDisplacement2, &ilLine))
		{
			std::string sname, sfile;
			wchar2strstring(sname, pSymbol->Name);
			wchar2strstring(sfile, ilLine.FileName);
			strStackInfo.Format(_T("FUN:%s() File:%s:[%d]"), sname.c_str(), sfile.c_str(), ilLine.LineNumber);
		}
		else
		{
			strStackInfo.Format(("query error: %d"), GetLastError());
		}
		vInfo.push_back(strStackInfo.GetBuffer());

	}
	FreeLibrary(hNt);
}
void PrintfStackInfo()
{
	printf("===========start===========\n");
	std::vector mVStackList;
	ShowTraceStack(mVStackList);
	for (int i = 0; i < mVStackList.size(); i++)
	{
		printf("%s\n", mVStackList[i].c_str());
	}
	printf("===========end===========\n");
}

测试结果:

打印的堆栈信息中包含函数名,文件名,行号

try catch无法捕获异常设置_第3张图片

 

 

 

 

你可能感兴趣的:(c++)