栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

在Windows/QT环境编程中同时catch C++异常和SEH异常

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Windows/QT环境编程中同时catch C++异常和SEH异常

代码如下:

//.h

class CExceptionGuard
{
public:
	CExceptionGuard();
	~CExceptionGuard();
private:
	class Impl;
	Impl* prd;
};


// .cpp
class SE_Exception : public std::exception
{
private:
	const unsigned int nSE;
public:
	SE_Exception() noexcept : SE_Exception{ 0 } {}
	SE_Exception(unsigned int n) noexcept : nSE{ n } {}
	unsigned int getSeNumber() const noexcept { return nSE; }
};

void trans_func(unsigned int u, EXCEPTION_POINTERS*)
{
	throw SE_Exception(u);
}

class Scoped_SE_Translator
{
private:
	const _se_translator_function old_SE_translator;
public:
	Scoped_SE_Translator(_se_translator_function new_SE_translator) noexcept
		: old_SE_translator{ _set_se_translator(new_SE_translator) } {}
	~Scoped_SE_Translator() noexcept { _set_se_translator(old_SE_translator); }
};

class CExceptionGuard::Impl
{
public:
	Impl() {};
	Scoped_SE_Translator scoped_se_translator {	trans_func};
};

CExceptionGuard::CExceptionGuard()
{
	prd = new Impl;
}
CExceptionGuard::~CExceptionGuard()
{
	delete prd;
	prd = NULL;
}


//.cpp


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class QxxxApplication : public QApplication
{
public:
	QxxxApplication(int& argc, char** argv, int = ApplicationFlags)
	    : QApplication(argc, argv, ApplicationFlags)
	{
	};

private:
	//同时可以处理C++异常和SEH异常。 注意: 界面线程所在项目设置中 "启用C++异常" 必须设定为 "是,但有 SEH 异常 (/EHa)"
	// lys @20220428 !!!
	virtual bool notify(QObject* receiver, QEvent* e)
	{
		try
		{
			return __super::notify(receiver, e);
		}
		catch (std::bad_exception& e)
		{
			std::wcout << L"std::bad_exception |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::bad_array_new_length& e)
		{
			std::wcout << L"std::bad_array_new_length |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::bad_alloc& e)
		{
			std::wcout << L"std::bad_alloc |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::bad_cast e)
		{
			std::wcout << L"std::bad_cast |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::bad_typeid e)
		{
			std::wcout << L"std::bad_typeid |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::logic_error e)
		{
			std::wcout << L"std::logic_error |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::runtime_error e)
		{
			std::wcout << L"std::runtime_error |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (std::exception& e)
		{
			std::wcout << L"std::exception |";
			std::wcout << foundation::to_wide_string(e.what());
			std::wcout << std::endl;
		}
		catch (...)
		{
			std::wcout << L"std::exception |";
			std::wcout << std::endl;
		}

		return false;
	}

};



int main(int argc, char *argv[])
{
	//同时可以处理C++异常和SEH异常。 注意: 界面线程所在项目设置中 "C++异常" 必须设定为 "是,但有 SEH 异常 (/EHa)"
	//lys @ 20220428
	//参考网页 https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-se-translator?view=msvc-170#:~:text=The%20_set_se_translator%20function%20provides%20a%20way%20to%20handle,a%20specific%20class%20type%20to%20a%20C%20exception.
	CExceptionGuard guard;

	std::wcout.imbue(std::locale("chs")); //支持输出中文 
	std::wcout << L"系统启动n";
    
    MainWindow w;
    w.showMaximized();

	QxxxApplication a(argc, argv);
	return a.exec();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/847378.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号