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++
{count} votes

Your answer

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