PDA

View Full Version : How to catch errors caused by non-valid return-values ?



homerun4711
8th January 2011, 09:31
Hello!

I have a question on how to catch errors if a return-value is not accessible. I am unsure which is a good way to make sure, that a correct value is returned and what to do, if that value is not available.

The function is used to fetch values from a QDialog, called on exec().



QSqlRecord SelectAddress::passRecord()
{
//check if something is selected
if(tableView->selectionModel()->hasSelection())
{
QModelIndex index = tableView->currentIndex();
if (index.isValid())
{
QSqlRecord record = modelCustomer->record(index.row());
return record;
}
}
else
{
QMessageBox::warning(0, QObject::tr("Selection error"),"No entry selected");
//application crashes here, what should be returned?
}


A point into the right direction would be enough.

Kind regards,
HomeR

Lykurg
8th January 2011, 09:46
I would return an empty record. It is up to the caller, to check if the returned value is correct. If you want to get the last error do something like:
QSqlRecord SelectAddress::passRecord()
{
//...
else
{
m_lastPassRecordError = tr("Nothing selected.");
return QSqlRecord();
}
}

QString SelectAddress::getLastPassRecordError()
{
return m_lastPassRecordError;
}

homerun4711
8th January 2011, 10:24
Thank you. Very good answer!