PDA

View Full Version : Exception-catching Error with Qt



Crc
13th December 2017, 07:28
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.


namespace Test{

class ExceptionEx; // Class which is inherited std::exception
// It, class ExceptionEx, is defined on another cpp file

class Class1{
public:
Class1() throw(ExceptionEx);
};

Class1::Class1() throw(ExceptionEx){
throw ExceptionEx( "exception" );
}

void func( void ){
try{
Class1 obj;
}catch( ExceptionEx &e ){
// print exception-string out
}
}
}

class CMain{
public:
CMain(){ this->createInstances(); this->setup(); } // To create widgets and other objects
... // omit...
protected:
void createInstances( void ){
// creating...
}

void setup( void ){
... // setup for widgets
Test::func(); // <- This line is wrong. It crashes.
}
private:
// member variables omitted...
};

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?

ChrisW67
14th December 2017, 00:08
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).


#include <iostream>
#include <exception>

namespace Test {
class ExceptionEx: public std::exception
{
public:
explicit ExceptionEx(const char* message): msg_(message) { }
explicit ExceptionEx(const std::string& message): msg_(message) {}
virtual ~ExceptionEx() throw () {}
virtual const char* what() const throw () { return msg_.c_str(); }
protected:
std::string msg_;
};
}

namespace Test{

class ExceptionEx; // Class which is inherited std::exception
// It, class ExceptionEx, is defined on another cpp file

class Class1{
public:
Class1() throw(ExceptionEx);
};

Class1::Class1() throw(ExceptionEx){
throw ExceptionEx( "exception" );
}

void func( void ){
try{
Class1 obj;
}catch( ExceptionEx &e ){
std::cout << "Failed" << std::endl;
}
}
}

int main(void)
{
Test::func();
}


I suspect the crash is coming from elsewhere.

Crc
21st December 2017, 07:22
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.