PDA

View Full Version : General C++ exception not working with QT Necessitas



blackrue2000
15th August 2012, 19:42
Could someone tell me how to handle exceptions with a QT necessitas mobile app. I have Math app that throws an exception if something is divided by zero. The code works perfectly on QT SDK for windows but when I run the program on necessitas the app always crashes. Why can't I throw an exception? Remember this code works fine on QT SDK, when I use Necessitas QT SDK the app crashes.

try
{
//Throw exception if divide by zero
if ((terms[0] == 0) || (terms[3]== 0) || (terms[6] == 0) || ((terms[9]-terms[11])== 0 ) || ((terms[12]+terms[14])== 0 ))
throw "Cannot divide by 0 term";


this->answerline1->setText(QString::number(((terms[1]+terms[2])/terms[0])));
this->answerline2->setText(QString::number(((terms[4]-terms[5])/terms[3])));
this->answerline3->setText(QString::number(((terms[7]*terms[8])/terms[6])));
this->answerline4->setText(QString::number((terms[10])/(terms[9]-terms[11])));
this->answerline5->setText(QString::number((terms[13])/(terms[12]+terms[14])));
}
catch(const char * message)
{
cerr << "Error " << message << endl;
}

d_stranz
17th August 2012, 17:57
I have also had trouble catching sigsegv (segmentation fault) exceptions (in Windows with Qt); the debugger reports something about exception handling via try/catch being incompatible with the signal / slot mechanism. As you are probably seeing, the catch clause is ignored even though the exception appears to be thrown, and the program crashes. In your case, simply replacing the try/catch with a conditional and a message box in case of error would solve your problem.

ChrisW67
17th August 2012, 21:51
See the other place you asked. Does that help?

amleto
17th August 2012, 23:32
I have also had trouble catching sigsegv (segmentation fault) exceptions (in Windows with Qt); the debugger reports something about exception handling via try/catch being incompatible with the signal / slot mechanism. As you are probably seeing, the catch clause is ignored even though the exception appears to be thrown, and the program crashes. In your case, simply replacing the try/catch with a conditional and a message box in case of error would solve your problem.

sigsev is not an exception that is thrown. It is your program going POP! You cant catch/recover from segfault.

d_stranz
19th August 2012, 00:35
sigsev is not an exception that is thrown. It is your program going POP! You cant catch/recover from segfault.

I oversimplified. What I actually did was to install a signal handler for sigsegv, and the handler threw an exception. The problem was that a third-party DLL over which I had no control (but which I had no choice except to use in my app) was blowing up with a segmentation fault. I needed to trap the fault, handle it, and let my app get on with life rather than dying a gory death. I could trap the signal just fine, but when I threw the exception out of it, Qt complained about that and the catch clause was never entered.

It is possible that the DLL also garbaged up the stack in the process of the segmentation fault, which then resulted in the failure of the catch clause to run, but that's impossible for me to find out since I can't debug it.