Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to let program stay live when an error happened?

how to let program stay live when an error happened?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johan
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      J 1 Reply Last reply
      0
      • mrjjM mrjj

        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?

        J Offline
        J Offline
        Johan
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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"

          mrjjM 1 Reply Last reply
          0
          • mrjjM mrjj

            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"

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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();
            }
            
            J 1 Reply Last reply
            0
            • mrjjM mrjj

              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();
              }
              
              J Offline
              J Offline
              Johan
              wrote on last edited by
              #6

              @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.

              mrjjM 1 Reply Last reply
              0
              • J Johan

                @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.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @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;
                    }
                
                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rondog
                  wrote on last edited by Rondog
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved