Your example code does not throw exceptions, it simply crashes (as you already observed). If you want to experiment with exceptions, replace your
QString a[3];
To copy to clipboard, switch view to plain text mode
by
#include <vector>
std::vector<QString> a(3); // 3 element vector filled with QString()
#include <vector>
std::vector<QString> a(3); // 3 element vector filled with QString()
To copy to clipboard, switch view to plain text mode
Next replace the access to the array elements
ui->label->setText(a[i++]);
ui->label->setText(a[i++]);
To copy to clipboard, switch view to plain text mode
by
ui->label->setText(a.at(i));
ui->label->setText(a.at(i));
To copy to clipboard, switch view to plain text mode
Note that using operator [] of std::vector does not do any range checking, while at() method will check the range and throw exception std:ut_of_range if the index is not inside the vector range. For more details see http://www.cplusplus.com/reference/vector/vector/at/.
Best regards
ars
Bookmarks