I have the following action which is executed when a certain button is pressed in a Qt application:
#include <shape.h>
void computeOperations()
{
polynomial_t p1("x^2-x*y+1"),p2("x^2+2*y-1");
BoundingBox bx(-4.01, 4.01,-6.01,6.01,-6.01,6.01);
Topology3d g(bx);
AlgebraicCurve* cv= new AlgebraicCurve(p1,p2);
g.push_back(cv);
g.run();
//Other operations on g.
}
Topology3d(...), AlgebraicCurve(..), BoundingBox(...), polynomial_t(...) are user defined types defined in the corresponding header file .
Now for some values of p1 and p2, the method g.run() works perfectly.
Thus for some other values of p1 and p2, g.run() it is not working anymore as the method gets blocked somehow and the message "Application Not Responding" appears and I have to kill the Application.
I would want to have the following behavior: whenever g.run() is taking too long, gets blocked for some particular values of p1, p2, I would want to display an warning box using QMessageBox::Warning.
I try to do this with try{...} and catch{...}:
#include <shape.h>
class topologyException : public std::runtime_error
{
public:
topologyException::topologyException(): std::runtime_error( "topology fails" ) {}
};
void computeOperations()
{
try
{
polynomial_t p1("x^2-x*y+1"),p2("x^2+2*y-1");
BoundingBox bx(-4.01, 4.01,-6.01,6.01,-6.01,6.01);
Topology3d g(bx);
AlgebraicCurve* cv= new AlgebraicCurve(p1,p2);
g.push_back(cv);
g.run();
//other operations on g
throw topologyException();
}
catch(topologyException& topException)
{
QMessageBox errorBox;
errorBox.setIcon(QMessageBox::Warning);
errorBox.setText("The parameters are incorrect.");
errorBox.setInformativeText("Please insert another polynomial.");
errorBox.exec();
}
}
This code compiles, but when it runs it does not really implement the required behavior.
For the polynomials for which g.run() gets blocked the error message box code is never reached, plus for the polynomials for which g.run() is working well, the error message box code still is reached somehow and the box appears in the application.
I am new to handling exceptions, so any help is more than welcomed.
I think the program gets blocked somewhere inside g.run() so it does not reach the exception, still I do not understand what really happens.
Still I would want to throw this exception without going into the code of g.run(), this function is implemented as part of a bigger library, which I just use in my code.
Can I have this behavior in my program without putting any try{...} catch{...} block statement in the g.run() function?