Try & Catch in QApplication



  • I try to manage the exception of my classes, and i put this "Throw" in the constructor.
    @
    line::line(point A, point B):a(A), b(B){
    if (a==b) {throw coincidentPointLine();}
    }
    @
    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:
    @void mainWindow::on_twoPointLineButton_clicked(){
    //get the input line
    QString Qinput = ui->lineEdit->text();
    std::string input = Qinput.toStdString();

    line* L;
    try{L = new line(GUIUtilities::getInputLine(input));}
    catch (coincidentPointLine()){
        std::cout<<"error";
        this->setResult(QString("Error Message!"));
        abort();}
    
    function F = L->lineEquatioon();
    std::string result = F.get[removed]);
    this->setResult(result);
    

    }@

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





  • Thanks! ;)


  • Moderators

    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/



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.