Exception Handing In QT C++
-
Hello Friends And QT Experts And Champions
Currently I Am working With Exception Handing In QT C++
i read many articles of Exception handing Or Application Crash Handing when Exception is Raise in My ApplicationMy Code Is :
In C++ File (Source File)
LONG MainWindow::AppCrashHandler(EXCEPTION_POINTERS *pException) { HANDLE hDumpFile = CreateFile((LPCWSTR)(QCoreApplication::applicationDirPath() + QDateTime::currentDateTime().toString("/yyyy-MM-dd-hh-mm-ss")+".dmp").utf16(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hDumpFile != INVALID_HANDLE_VALUE) { MINIDUMP_EXCEPTION_INFORMATION dumpInfo; dumpInfo.ExceptionPointers = pException; dumpInfo.ThreadId = GetCurrentThreadId(); dumpInfo.ClientPointers = TRUE; MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL); } EXCEPTION_RECORD* record = pException->ExceptionRecord; QString errCode(QString::number(record->ExceptionCode, 16)), errAdr(QString::number((uint)record->ExceptionAddress, 16)), errMod; QMessageBox::critical(nullptr, "Error", QString("Error Code: %1 Error Address: %2").arg(errCode).arg(errAdr),QMessageBox::Ok); return EXCEPTION_EXECUTE_HANDLER; } void MainWindow::on_pushButton_clicked() { __try { int a = 10; int b = 0; int c = 0; c = a / b; std::cout << c; } __except(faultHandler(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) { } } void MainWindow::on_pushButton_2_clicked() { __try { std::string *temp = new std::string[100]; temp[0] = "ABCD"; temp[1] = "ABCDEFGH"; temp[2] = "ABCDEFGHIJKL"; temp[150] = "ABCDEFGHIJKLMNOP"; std::cout << temp[0]; std::cout << temp[1]; std::cout << temp[2]; std::cout << temp[150]; } __except(faultHandler(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER) { } }
You can see in my code i am trying To Handle some types of exception like in DivideByZero OR Invalid Memory Pointer Access
My Problem is i can't use Qt Class or Object Like QString , QSqlDatabase Class Or etc...
Whenever I am trying To use Qt Class or Object in __try Block that time it wiil display error message
*** Error Message ***
error: C2712: Cannot use __try in functions that require object unwindingPlease Help me to solve this problem OR suggest me any other way to handle Exception at runtime And if you have any other idea or Solution so please drop hear.
Thanks you so much To All
-
@Ketan__Patel__0011
I don't see this has much to do with Qt.99% of your code has nothing Qt in it --- just C++/
std::
library.Since your
AppCrashHandler()
is entirely Windows-specific, I would get rid of it using Qt classes likeQDateTime
orQString
, and replaceQMessageBox
with the native Windows SDKMessageBox
, and then it's all standalone Windows code and you won't have Qt issues....P.S.
Having said the above --- which if I were you I would do anyway --- I don't think your Qt code which is only inAppCrashHandler()
has anything to do with the error message. I thought that would come from e.g. yourstd::string *temp
. Unless you are choosing to show code which does not produce the error message...?It should be only from things directly inside the
__try
? And if you insist on having Qt/std::
/whatever there, if you can push that down into a function call instead I think the error goes away? -
Structured Exception Handling, or SEH in short (the
__try
/__except
things) is a Windows specific extension to the language and is not very well suited for complex C++ scenarios. It has many limitations, like the one you see. As the message says you can't use it in context where an object with a destructor is present. A common workaround is to move the__try
/__except
code to a separate function, so for example instead of this:void func() { SomeComplexObjectLikeQObject obj; __try { ... } __except(...) { ... } }
do something like this:
void workaround() { __try { ... } __except(...) { ... } } void func() { SomeComplexObjectLikeQObject obj; workaround(); }
All in all SEH is intended rather for a C style code that integrates with WinAPI. If possible try switching to the standard C++ exception handling (
try
/catch
) and you'll avoid a lot of gotchas. -
@Chris-Kawa
Thanks For Your Reply
Before __try and __except I was using try and catch but i didn't work for handle Runtime Exception
that's why i am switch to __try and __except -
Yup, some stuff is not handled by exceptions in C++, just specified as undefined behavior. That behavior might be defined for given platform though, like in POSIX or, in this case, Windows.
Ok, so just keep in mind that you can't have complex objects in the same function as__try
/__except
. My example was with an object outside the try block, but its the same for objects inside. Just move the body of a try block to a separate function in that case. -
@Ketan__Patel__0011 said in Exception Handing In QT C++:
Before __try and __except I was using try and catch but i didn't work for handle Runtime Exception
Just to say: not sure what you're hoping to do handling RTEs much? Why are you getting them? Are you allowing your code to continue after catching one?
-
@Ketan__Patel__0011
Then you will still have to verify that it is safe, and all is actually well, to continue after the particular exception. Just saying, it may or may not be.