【发布时间】:2009-10-09 11:47:56
【问题描述】:
我有以下动作,当某个特定的时候执行 在Qt 应用程序中按下按钮:
#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(...) 是在 对应的头文件。
现在对于 p1 和 p2 的某些值,g.run() 方法可以完美运行。
因此对于 p1 和 p2 的一些其他值,g.run() 不是 随着该方法以某种方式被阻塞并且 出现消息“应用程序无响应”,我必须 杀死应用程序。
我希望有以下行为:无论何时 g.run() 耗时太长,因某些特定原因被阻塞 p1, p2 的值,我想显示一个警告框 使用 QMessageBox::Warning。
我尝试用 try{...} 和 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();
}
}
这段代码可以编译,但运行时并没有真正实现 实现所需的行为。
对于 g.run() 被阻止的多项式错误 永远不会达到消息框代码,加上多项式 对于哪个 g.run() 运行良好,错误消息框 代码仍然以某种方式到达,并且该框出现在 应用。
我是处理异常的新手,所以任何帮助都不仅仅是 欢迎。
我认为程序在 g.run() 中的某个地方被阻塞了,所以 它没有达到异常,我还是不明白 到底发生了什么。
我仍然想不去就抛出这个异常 进入 g.run() 的代码,这个函数被实现为 一个更大的库的一部分,我只是在我的代码中使用它。
我可以在我的程序中有这种行为而不添加任何内容吗 g.run() 函数中的 try{...} catch{...} 块语句?
【问题讨论】: