Results 1 to 3 of 3

Thread: How to catch errors caused by non-valid return-values ?

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default How to catch errors caused by non-valid return-values ?

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

    Qt Code:
    1. QSqlRecord SelectAddress::passRecord()
    2. {
    3. //check if something is selected
    4. if(tableView->selectionModel()->hasSelection())
    5. {
    6. QModelIndex index = tableView->currentIndex();
    7. if (index.isValid())
    8. {
    9. QSqlRecord record = modelCustomer->record(index.row());
    10. return record;
    11. }
    12. }
    13. else
    14. {
    15. QMessageBox::warning(0, QObject::tr("Selection error"),"No entry selected");
    16. //application crashes here, what should be returned?
    17. }
    To copy to clipboard, switch view to plain text mode 

    A point into the right direction would be enough.

    Kind regards,
    HomeR

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to catch errors caused by non-valid return-values ?

    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:
    Qt Code:
    1. QSqlRecord SelectAddress::passRecord()
    2. {
    3. //...
    4. else
    5. {
    6. m_lastPassRecordError = tr("Nothing selected.");
    7. return QSqlRecord();
    8. }
    9. }
    10.  
    11. QString SelectAddress::getLastPassRecordError()
    12. {
    13. return m_lastPassRecordError;
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    homerun4711 (8th January 2011)

  4. #3
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default Re: How to catch errors caused by non-valid return-values ?

    Thank you. Very good answer!

Similar Threads

  1. Replies: 4
    Last Post: 1st February 2010, 14:21
  2. Stored procedure return values problem
    By jgreetham in forum Qt Programming
    Replies: 7
    Last Post: 10th September 2007, 17:38
  3. how to program custom Dialogs with return values
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 23rd December 2006, 00:37
  4. Crash caused by QVariant (mis)use
    By mclark in forum Newbie
    Replies: 2
    Last Post: 31st October 2006, 15:05
  5. Weird crash caused by a QTreeView
    By vfernandez in forum Qt Programming
    Replies: 1
    Last Post: 10th September 2006, 18:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.