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.
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