When handling c++ iterator, try cath exception is not working.

Junyoung Lee 1 Reputation point
2022-07-22T02:20:42.217+00:00

I am new to c++. And I am making a qt program using c++.
In the code below, a try catch statement is used to handle an exception that exceeds the index of the list when processing the loop.
However, when an error occurs in the try statement, it cannot be caught and an error is raised and the program ends. Can you tell me what's wrong with my code?

QListWidget* returnListWidget(list<QListWidget*> srclist, int num) {  
      
    try {  
        list<QListWidget*>::iterator iter = srclist.begin();  
        advance(iter, num);  
        //error occur   
        QListWidget* q = *iter;  
        return q;  
    }  
    catch (...) {  
        qDebug() << "helo";  
        return NULL;  
    }  
}  

223492-picture.jpg

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.