August 17, 2012

rikytres rikytres
Lab Rat
29 posts

Try & Catch in QApplication

 

I try to manage the exception of my classes, and i put this “Throw” in the constructor.

  1. line::line(point A, point B):a(A), b(B){
  2.     if (a==b)   {throw coincidentPointLine();}
  3. }

This is in the logical part of the my project and there’s no Qt object of function in this class.

And this is a slot of my QMainWindow:

  1. void mainWindow::on_twoPointLineButton_clicked(){
  2.     //get the input line
  3.     QString Qinput = ui->lineEdit->text();
  4.     std::string input = Qinput.toStdString();
  5.    
  6.     line* L;
  7.     try{L = new line(GUIUtilities::getInputLine(input));}
  8.     catch (coincidentPointLine()){
  9.         std::cout<<"error";
  10.         this->setResult(QString("Error Message!"));
  11.         abort();}
  12.    
  13.     function F = L->lineEquatioon();
  14.     std::string result = F.get[removed]);
  15.     this->setResult(result);
  16. }

It’s look like it woks but doesn’t! The compiler return this error!
*Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.*

How i can resolve this problem?
I read about QApplication::notify(), but i don’t understand very well…

4 replies

August 17, 2012

Rahul Das Rahul Das
Robot Herder
362 posts

this [qt-project.org] and this [doc.qt.nokia.com]

must be useful..

 Signature 

——————————-

    Rahul Das

——————————-

August 17, 2012

rikytres rikytres
Lab Rat
29 posts

Thanks! ;)

August 18, 2012

Tobias Hunger Tobias Hunger
Mad Scientist
3150 posts

You better change your code to not throw from the constructor. How do you reclaim the memory used by a object that threw in its constructor? It is not constructed yet, so there is no object. There is nothing you can call the destructor on.

See e.g. http://herbsutter.com/2008/07/25/constructor-exceptions-in-c-c-and-java/

August 18, 2012

Duck Duck
Lab Rat
107 posts

Your problem has nothing to do with Qt.

“catch (coincidentPointLine())” catches exceptions of type function-without-parameters-returning-coincidentPointLine. You are throwing objects of type coincidentPointLine. The types are unrelated.

 
  ‹‹ Closing a QDialog from another class      [Solved] Linking in a static library ››

You must log in to post a reply. Not a member yet? Register here!