how to let program stay live when an error happened?



  • For example, the program read a file, generate an error message, how to make the program live?

    I set the exception for this condition, program will show a message box tell user there is an error happened when read file. After clicking OK button, the program died.

    what I want, just keep the program running.


  • Qt Champions 2016

    Hi
    if you throw an exception,
    you must catch it again.
    else abort is called and program terminated:

    Not sure that is what you are seeing.
    Qt doesn't use exceptions, only error codes.

    So if you are not throwing exception yourself ,
    then you might have a crash somewhere.

    do you have some code that does this?



  • @mrjj

    void MainWindow::ReadImage()
    {
        img=imread(filename,4);
        if(img.empty())
        {
            QMessageBox msg;
            msg.setText("there is no data in this image");
            msg.exec();
        }
    }
    

    Here, I post a simple code explain my question, when the program did not read correct file, a message box will pop up, and tell user somethings.
    but when click OK in the message box, the program is going to die. I hope the program can live and let the user select a right image.


  • Qt Champions 2016

    Hi
    Super
    Normally Mainwin dont close after MessageBox :)

    What does imread(filename,4) returns?

    Do you use img after messagebox somewhere else?

    If you look in Application output, does it say something like
    "the program has unexpectedly finished"


  • Qt Champions 2016

    Hi
    Also, is this a normal project ?
    Meaning the main.cpp is the normal one:

    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    


  • @mrjj

    img=imread(filename,4);
    

    is a opencv code. I just use Qt to make a GUI for it.
    I do not know where is wrong and also do not know why is it working now. I tried to arrange some image processing code which probably cause this problem.

    Base on what you said, I guess the problem is, there are still some code need to be executed after the messagebox pop up.

    Thank you very much.


  • Qt Champions 2016

    @Johan said:

    oh in that case it returns cv::Mat so maybe if used later on
    opencv will throw exception ?
    It would then close the QT app if you do not catch it.

     try{
         // openvc code
        }
        catch(Exception ex){
           cout<< ex.msg;
        }
    


  • I agree with the exception approach (try, catch).

    If you write something complex with lots of levels but related to a specific area using exceptions make a lot of sense. I use this when reading XML files for example as you frequently use sub functions and recursion to process this data. Any alternate method would be a real nightmare to try and track down the source of a problem.

    As far as having a surviving program it depends on how thorough you are with testing of input parameters. If, for example, you never test for a null pointer in a function then it will crash the program if you happen to have a null pointer. You need to be careful about what assumptions you make.

    Although it is fatal to a program I like to use assert statements if nothing else. Your program will immediately crash but at least you know where (and possibly why) if you use this. It is better then trying to figure out why some seemingly unrelated function crashes after the real cause of the problem.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.