Results 1 to 2 of 2

Thread: How to catch out of range array crashed error?

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default How to catch out of range array crashed error?

    Hi,

    After I pressed pushbutton (four times) , variable i is reach 3 (a array is out of range so) application is crashed.
    I don't fix this error with checking of array size. I only want to use try .. catch in sample

    Qt Code:
    1. //header file
    2. QString a[3];
    3. int i;
    4.  
    5. //cpp file
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. a[0]="Zero";
    12. a[1]="Three";
    13. a[2]="Two";
    14. i=0;
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::on_pushButton_clicked()
    23. {
    24. try {
    25. ui->label->setText(a[i++]);
    26. }
    27. catch(std::exception &e){
    28. qDebug() << "error1" << endl;
    29. }
    30. catch (...)
    31. {
    32. qDebug() << "error2" << endl;
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 10th October 2015 at 23:56. Reason: changed [quote] to [code]

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to catch out of range array crashed error?

    Your example code does not throw exceptions, it simply crashes (as you already observed). If you want to experiment with exceptions, replace your
    Qt Code:
    1. QString a[3];
    To copy to clipboard, switch view to plain text mode 
    by
    Qt Code:
    1. #include <vector>
    2. 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
    Qt Code:
    1. ui->label->setText(a[i++]);
    To copy to clipboard, switch view to plain text mode 
    by
    Qt Code:
    1. 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

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

    binary001 (11th October 2015)

Similar Threads

  1. Replies: 3
    Last Post: 27th July 2011, 10:30
  2. Replies: 6
    Last Post: 8th April 2011, 22:15
  3. any way to catch error message from dll in a gui app?
    By lvdong in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2010, 11:49
  4. HTTP Range Option Error 206
    By nhs_0702 in forum Newbie
    Replies: 1
    Last Post: 21st April 2010, 13:25
  5. Error message: The Application crashed
    By josecarlosmissias in forum Newbie
    Replies: 13
    Last Post: 10th December 2009, 17:38

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.