Results 1 to 3 of 3

Thread: Exception-catching Error with Qt

  1. #1
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Exception-catching Error with Qt

    I'm Japanese, so I'll maybe write wrong English.

    I have a question.
    I cannot throw exceptions on Qt.
    For example, I attempted this code.

    Qt Code:
    1. namespace Test{
    2.  
    3. class ExceptionEx; // Class which is inherited std::exception
    4. // It, class ExceptionEx, is defined on another cpp file
    5.  
    6. class Class1{
    7. public:
    8. Class1() throw(ExceptionEx);
    9. };
    10.  
    11. Class1::Class1() throw(ExceptionEx){
    12. throw ExceptionEx( "exception" );
    13. }
    14.  
    15. void func( void ){
    16. try{
    17. Class1 obj;
    18. }catch( ExceptionEx &e ){
    19. // print exception-string out
    20. }
    21. }
    22. }
    23.  
    24. class CMain{
    25. public:
    26. CMain(){ this->createInstances(); this->setup(); } // To create widgets and other objects
    27. ... // omit...
    28. protected:
    29. void createInstances( void ){
    30. // creating...
    31. }
    32.  
    33. void setup( void ){
    34. ... // setup for widgets
    35. Test::func(); // <- This line is wrong. It crashes.
    36. }
    37. private:
    38. // member variables omitted...
    39. };
    To copy to clipboard, switch view to plain text mode 

    The program crashed when I call the function Test::func.

    Cannot we throw exceptions on Qt?

    I have libraries for me, link it and the Boost.
    If we cannot throw exceptions on Qt, I cannot use Qt...

    Strictly speaking, cannot we *catch* exceptions with Qt?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Exception-catching Error with Qt

    Your question (and code) have nothing to do with Qt. Using the Qt library does not change the handling of C++ exceptions. The following code based on yours does not crash and outputs "Failed" here (GCC 6.4, nothing to do with Qt).
    Qt Code:
    1. #include <iostream>
    2. #include <exception>
    3.  
    4. namespace Test {
    5. class ExceptionEx: public std::exception
    6. {
    7. public:
    8. explicit ExceptionEx(const char* message): msg_(message) { }
    9. explicit ExceptionEx(const std::string& message): msg_(message) {}
    10. virtual ~ExceptionEx() throw () {}
    11. virtual const char* what() const throw () { return msg_.c_str(); }
    12. protected:
    13. std::string msg_;
    14. };
    15. }
    16.  
    17. namespace Test{
    18.  
    19. class ExceptionEx; // Class which is inherited std::exception
    20. // It, class ExceptionEx, is defined on another cpp file
    21.  
    22. class Class1{
    23. public:
    24. Class1() throw(ExceptionEx);
    25. };
    26.  
    27. Class1::Class1() throw(ExceptionEx){
    28. throw ExceptionEx( "exception" );
    29. }
    30.  
    31. void func( void ){
    32. try{
    33. Class1 obj;
    34. }catch( ExceptionEx &e ){
    35. std::cout << "Failed" << std::endl;
    36. }
    37. }
    38. }
    39.  
    40. int main(void)
    41. {
    42. Test::func();
    43. }
    To copy to clipboard, switch view to plain text mode 

    I suspect the crash is coming from elsewhere.
    Last edited by ChrisW67; 13th December 2017 at 23:18.

  3. #3
    Join Date
    Nov 2017
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Exception-catching Error with Qt

    Thank you for your answer.

    I had a mistake to send which I should do.

    Real is...

    https://pastebin.com/iepQ4pqQ

    On the code, QtUse::QtWorker::CApplication is a class which derived QApplication,
    QtUse::QtWindow::MainWindow is a class which derived QMainWindow...

    Because they don't have event code.
    So, I derived QMainWindow and create QtUse::QtWindow::MainWindow with event-processing.

    You can read it as QMainWindow.

    QtUse::QtMenu::Menu is composition with QMenu, QtUse::QtMenu::MenuItem is derived from QAction.

    Thank you.

Similar Threads

  1. Replies: 1
    Last Post: 22nd September 2016, 17:38
  2. Replies: 1
    Last Post: 25th November 2013, 04:59
  3. error: A data abort exception has occurred
    By hisong1988 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 13th January 2011, 01:08
  4. exception catching speed
    By stefan in forum General Programming
    Replies: 10
    Last Post: 12th November 2008, 19:58
  5. Exception Error in my application.
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2008, 12:02

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.