代码如下:
//.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();
}
|