Results 1 to 13 of 13

Thread: handling exceptions in Qt

  1. #1
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default handling exceptions in Qt

    Hi Everyone,

    We are working on embedded linux platform. We use .wav files for our audio purpose. Recently due to some reason few wav files had got corrupted and when we were played them the application crashed giving the following exception:- "Floating point exception". Then when we replaced those wav files with new ones it again became fine.
    My doubt is not exactly related to this exception but related to the crashing of application when such exceptions arise.
    So can someone suggest us some effective means/ways to handle such exceptions which ultimately lead to crashing of the main application?



    -Thanking everyone
    Sattu

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: handling exceptions in Qt

    Qt does not control the exceoptions in C/C++ code. Exceptions / traps / signals are just as in plain C/C++.

    There are some compiler options to get such exception signals....Try googling "floating point exception", lot of stuff is there.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Quote Originally Posted by Santosh Reddy View Post
    Try googling "floating point exception", lot of stuff is there.
    Thanks but as I mentioned my doubt is not exactly related to this particular exception but how to prevent the crashing of my app once such exceptions arise. But again as you mentioned Qt doesn't deal with it as it comes from C++ code. So I guess I ultimately have to google

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: handling exceptions in Qt

    You can use signal() or sigaction() to install a handler for SIGFPE and SIGSEGV however be aware that when receiving such signal your application may be in an undefined state so the only thing you should do at this point is to gracefully quit the application. SIGFPE has nothing to do with C/C++ exceptions.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    sattu (28th January 2013)

  6. #5
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Quote Originally Posted by wysota View Post
    ......... receiving such signal your application may be in an undefined state so the only thing you should do at this point is to gracefully quit the application
    Thanks a lot Wysota. But what do you mean exactly by "application may be in an undefined state"? I mean can we atleast popup a message showing the reason of the crash to the end user and write the same to a log file before quitting the application?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: handling exceptions in Qt

    Quote Originally Posted by sattu View Post
    But what do you mean exactly by "application may be in an undefined state"?
    Applications are crashing not because people who implemented your operating system were malicious, but rather that when your application does something horribly bad, the best solution is to kill it as soon as possible to prevent serious damage to hardware, system or even your application data. When the system detects such dangerous situation, it sends a signal to the process (most often SIGSEGV, SIGFPE or SIGBUS) and aborts it right after. This may happen in any moment of execution of your program, for instance right after you tried to overwrite your own stack or even your own program code, effectively destroying data critical for the process to work. Thus you should consider any internal state of the program to be corrupt and unrecoverable.

    I mean can we atleast popup a message showing the reason of the crash to the end user and write the same to a log file before quitting the application?
    It should be safe to write something to a file. Doing anything with the GUI is not safe. If you are after playing some sound then you can play it using an external process (e.g. external player). This way, even if it crashes, your main program can continue safely.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    sattu (28th January 2013)

  9. #7
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Quote Originally Posted by wysota View Post
    the best solution is to kill it as soon as possible to prevent serious damage to hardware, system or even your application data.
    Actually Wysota this problem is not exactly from application side because the wav files got corrupted. That's why I wanted to know how to handle such situations as they are not in our control.

    If you are after playing some sound then you can play it using an external process (e.g. external player). This way, even if it crashes, your main program can continue safely.
    Correct but then again not an option in our case . Sticking to QSound as of now

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: handling exceptions in Qt

    Quote Originally Posted by sattu View Post
    Actually Wysota this problem is not exactly from application side because the wav files got corrupted.
    Of course it is the fault of the application. The library you are using for playing sounds apparently can't handle invalid files and causes a crash.

    Correct but then again not an option in our case
    Why not?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Quote Originally Posted by wysota View Post
    Of course it is the fault of the application. The library you are using for playing sounds apparently can't handle invalid files and causes a crash.
    Well, we are using QSound. So, maybe I will see if I can work on the library part

  12. #10
    Join Date
    Oct 2010
    Location
    Bangalore
    Posts
    52
    Thanks
    8
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    with QProcess you can restart your appliaction when it crashed this is most useful in embedded linux .
    Example:
    Qt Code:
    1. #include "applicationcontrol.h"
    2.  
    3. ApplicationControl::ApplicationControl(QObject *parent) :
    4. QObject(parent)
    5. {
    6.  
    7.  
    8. m_pProcess = new QProcess*[2];
    9.  
    10. for(int i = 0; i < 2; i++)
    11. {
    12. m_pProcess[i] = new QProcess();
    13.  
    14. m_pProcess[i]->setProcessChannelMode(QProcess::MergedChannels);
    15. connect(m_pProcess[i], SIGNAL(finished(int, QProcess::ExitStatus)),this, SLOT(processFinished(int, QProcess::ExitStatus)));
    16. connect(m_pProcess[i], SIGNAL(error(QProcess::ProcessError)),this, SLOT(processError(QProcess::ProcessError)));
    17. }
    18.  
    19. m_pProcess[0]->setObjectName("Server");
    20. m_pProcess[1]->setObjectName("Client");
    21.  
    22.  
    23.  
    24. QTimer::singleShot(5,this,SLOT(StartJob()));
    25.  
    26. m_nServerStarted = false;
    27. m_nClientStarted = false;
    28. }
    29. void ApplicationControl::StartJob()
    30. {
    31. m_pProcess[0]->start("/root/CCU/Server");
    32. m_pProcess[1]->start("/root/CCU/Client");
    33.  
    34. }
    35.  
    36. void ApplicationControl::processError(QProcess::ProcessError error)
    37. {
    38.  
    39. QProcess *pProcess = qobject_cast<QProcess*>(sender());
    40.  
    41. qDebug() << "Application Name " << pProcess->objectName();
    42.  
    43. if (error == QProcess::FailedToStart)
    44. {
    45. qDebug(("Unable to Start Job ..."));
    46. return ;
    47. }
    48. else if (error == QProcess::Crashed)
    49. {
    50.  
    51. qDebug() << "Program Crashed error " << error << QDateTime::currentDateTime().toString();
    52. }
    53. else if (error == QProcess::Timedout)
    54. {
    55. qDebug() << "Program Timedout error " << error << QDateTime::currentDateTime().toString();
    56.  
    57. }
    58. else if (error == QProcess::WriteError)
    59. {
    60. qDebug() << "Program WriteError error " << error << QDateTime::currentDateTime().toString();
    61.  
    62. }
    63. else if (error == QProcess::ReadError)
    64. {
    65. qDebug() << "Program ReadError error " << error << QDateTime::currentDateTime().toString();
    66.  
    67. }
    68. else
    69. {
    70. qDebug() << "Program Unknow error " << error << QDateTime::currentDateTime().toString();
    71.  
    72. }
    73.  
    74. }
    75. void ApplicationControl::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
    76. {
    77. /* If crashed Restart Application */
    78. QProcess *pProcess = qobject_cast<QProcess*>(sender());
    79. qDebug() << "Application Name " << pProcess->objectName();
    80. if(exitStatus == 0)
    81. {
    82. qDebug() << "exit Success Restarting " << pProcess->objectName();
    83.  
    84. return ;
    85.  
    86. }
    87.  
    88. if (exitStatus == QProcess::CrashExit)
    89. {
    90. qDebug() << "Program Crashed " << QDateTime::currentDateTime().toString();
    91. // return ;
    92.  
    93. }
    94. else if (exitCode != 0)
    95. {
    96. qDebug() << "Failed to Start" << QDateTime::currentDateTime().toString();
    97. }
    98.  
    99. if(pProcess->objectName() == "Server")
    100. {
    101. m_pProcess[0]->start("/root/CCU/Server");
    102. }
    103. else if(pProcess->objectName() == "Client")
    104. {
    105. m_pProcess[1]->start("/root/CCU/Client");
    106. }
    107.  
    108.  
    109. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by pradeepreddyg95; 28th January 2013 at 16:43.

  13. The following user says thank you to pradeepreddyg95 for this useful post:

    sattu (29th January 2013)

  14. #11
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Wow, thanks Pradeep. This code looks really great, I will try implementing it in my context.


    Added after 17 minutes:


    Quote Originally Posted by wysota View Post
    you can play it using an external process (e.g. external player). This way, even if it crashes, your main program can continue safely.
    Hi Wysota, I tried playing wav files in a different process but I am encountering certain problem in it. Here's the way I implemented it:-

    Qt Code:
    1. [QUOTE].h file[/QUOTE]
    2. class customWavePlayer:public QSound
    3. {
    4. public:
    5. customWavePlayer(const QString&, QObject*);
    6. ~customWavePlayer();
    7. bool playWaveFile(QString filename, int vol);
    8. };
    9.  
    10. [QUOTE].cpp file[/QUOTE]
    11. customWavePlayer::customWavePlayer(const QString& str, QObject* parent):QSound(str,parent)
    12. {
    13. }
    14.  
    15. customWavePlayer::~customWavePlayer()
    16. {
    17. }
    18.  
    19. bool customWavePlayer::playWaveFile(QString filename, int vol)
    20. {
    21. static int child_pid=-1;
    22. int status;
    23. if(child_pid>0)
    24. {
    25. waitpid(child_pid, &status, 0);
    26. }
    27. child_pid=fork();
    28. if(child_pid==0) //in child
    29. {
    30. qDebug("******************started playing");
    31. play(filename,vol);
    32. //while(!isFinished());
    33. qDebug("******************finished playing");
    34. //exit(EXIT_SUCCESS);
    35. }
    36. if(child_pid!=-1)
    37. return true;
    38. else
    39. return false;
    40. }
    To copy to clipboard, switch view to plain text mode 

    By this the file is playing but the application hangs. For more information our's is a multi-threaded GUI application. 4-5 different threads are running in the main application.
    Can you suggest us some better way to play it in a different process?
    Last edited by sattu; 29th January 2013 at 07:54.

  15. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: handling exceptions in Qt

    You don't need to code your own player. I'm sure there are some available for your platform. Just install one and use it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #13
    Join Date
    Sep 2010
    Location
    Bangalore
    Posts
    169
    Thanks
    59
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: handling exceptions in Qt

    Quote Originally Posted by wysota View Post
    You don't need to code your own player. I'm sure there are some available for your platform. Just install one and use it.
    Well, ok. Let me see to it.

Similar Threads

  1. gcc exceptions
    By ctarsoaga in forum General Programming
    Replies: 1
    Last Post: 4th May 2010, 07:37
  2. gcc exceptions
    By ctarsoaga in forum Qt Programming
    Replies: 0
    Last Post: 12th April 2010, 20:58
  3. handling exceptions
    By mohanakrishnan in forum Qt Programming
    Replies: 3
    Last Post: 25th November 2009, 12:33
  4. Using exceptions with Qt
    By vu64 in forum Qt Programming
    Replies: 1
    Last Post: 17th May 2009, 03:28
  5. sql Exceptions
    By peace_comp in forum Qt Programming
    Replies: 2
    Last Post: 10th October 2008, 02:51

Tags for this Thread

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.