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

24 篇文章 1 订阅
13 篇文章 0 订阅

代码如下:


//.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 <chrono>
#include <conio.h>
#include <iostream>
#include <string>
#include <thread>
#include <QtCore/QTimer>
#include <QtWidgets/QApplication>
#include <exception>
#include <iostream>
#include <QtWidgets/QApplication>

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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值