I need to restart the application when my application get crash. QProcess have finished() signal which gives the exit status as normal or crash. Like i need to get exit status for QWidget.
I need to restart the application when my application get crash. QProcess have finished() signal which gives the exit status as normal or crash. Like i need to get exit status for QWidget.
QWidget is not your application and does not have an "exit status". A class derived from QCoreApplication provides an exit status through the return value of QCoreApplication::exec(). It has a QCoreApplication::aboutToQuit() signal that is issued on a normal exit.
An application usually crashes due to some abnormal occurrence that your code does not trap. This is often the result of a signal. You can install a signal handler to trap the signal, but that doesn't mean you can do anything about it other than make a more graceful exit from your program.
Why don't you find and fix the cause of your program crashes instead? Good programs never crash, because the programmers who write them take care to write code that checks for errors instead of just letting them happen.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
yes QCoreApplication::aboutToQuit signal get emit , if it normally closed. But I need to handle the unexpected error or crash. Suppose consider the code as
In my main.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}
In Widget.cpp :
void Widget:: on_pushButton_clicked()
{
QStringList list;
QString data = list[0]; // index out of range
}
This will cause crash. I need to handle this error by catching the error. Instead of showing the crashing window and i need to show any error messge.
Hi,
As d_stranz pointed you, is it better to catch this error and do not let it happen
Would be much better. But as you declare "list" as an empty QStringList, "data = list.at(0);" will never be executed.Qt Code:
QStringList list; QString data; if (list.count() > 0) data = list.at(0);To copy to clipboard, switch view to plain text mode
Òscar Llarch i Galán
Yes I know it gives run time while accessing the 0th item in list. My need is, i want to handle this type of error in application. Instead of crashing, i need to show a error message for this case. Thank you.
In your original message you wrote that your need is to restart, which can easily be done with QProcess.
Now you write "instead of crashing".
The only way not to crash is not do things that make the program crash.
A crash is program state where the operating system can no longer execute the program, one cannot "recover" from a crash.
Cheers,
_
Hi,
Do you know the "else" statement?Instead of crashing, i need to show a error message for this case
Something like
Qt Code:
if (list.count() > 0) data = list.at(0); else //Show an alert message here!To copy to clipboard, switch view to plain text mode
Òscar Llarch i Galán
This doesn't make sense?
If it is crashing at line 2, then it will never reach the else.
You need to sanity check your list before it tries to assign from the array into data.
You should breakpoint before the crash occurs, and evaluate the list expression, then write some code to deal with that. (Unless I've misunderstood where the crash is occurring).
But if the question is what to show for a message box, use something like QMessageBox.
The OP's code crashed because it was trying to access an entry from an empty list, so list.at( anything ) will fail. The code posted by NyAw checks to see that the list is not empty before trying to access the first element, so it should not crash. Even if the first element is empty, it will still return an empty QString for assignment into data. If the list itself is empty (count() == 0) then the else clause will be executed. That -is- the "sanity check".If it is crashing at line 2, then it will never reach the else.
If accessing a valid entry from a non-empty list could cause a crash (unlikely), then you can guard against that by enclosing the entire if /else conditional in an appropriate try / catch clause.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks