where am I supposed to catch it?
这就是为什么Qt不支持跨信号/插槽连接抛出异常。如果你尝试,你会看到这样一条消息:
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.
因为它提到,有可能继承的QApplication和赶上你的例外存在,但是这将是处理事情非常恼人的方式。
如果可能的话,我会建议重写量,使得它不会抛出。
如果你不能重写count()?
例如,如果计数()是在第三方库中的函数的一部分,你使用的是什么?
在任何官方Qt库没有插槽抛出,因此,如果您使用的是第三方的库抛出一个插槽,它可能是一个迹象,表明它不是一个好的图书馆。如果一定要使用的话,我建议,而不是QApplication::notify
捕捉它,你不是创建一个适配器。
这是什么意思?首先创建一个对象,在构造函数中使用粗略的第三方对象。在它中,写一个插槽,用try/catch块封装一个调用投掷槽。现在,而不是连接到粗略第三方对象的插槽,连接到您的新创建的对象的插槽。
通过这种方式捕获异常会将相关代码保存在一起,并且如果遇到多个这些有问题的函数时,阻止QApplication::notify
填充大量不相关的try/catch块。
例如:
class BadCounter {
Q_OBJECT
public slots:
void count() { throw CounterError("unable to count"); }
};
class CounterAdaptor {
Q_OBJECT
BadCounter* counter_;
public:
CounterAdaptor(BadCounter* counter) {
counter_ = counter;
}
public slots:
void count() {
try {
counter_->count();
} catch (const CounterError& e) {
std::cerr << e.what() << std::endl;
}
}
};
int main() {
BadCounter engine;
CounterAdaptor adaptor(&engine);
QThread* thread = new QThread();
connect(thread,SIGNAL(started()),&adaptor,SLOT(count()));
thread.start();
... // etc...
delete thread;
}
如果你要处理的东西,可以从任何地方被抛出?
这种全球关注的最明显的例子是意外的异常。错误可能发生在任何地方。尽可能多地记录事件的细节是可取的,因此可以识别和纠正原因。在这种情况下,你会希望中所示jichi's answer在自己的子类重新实现QApplication::notify
。使用全局处理程序来处理全局问题是非常合理的。
也许你应该把try-catch块在count()函数。 .. – Kobe 2012-04-09 15:52:47
#vBx计数抛出 – smallB 2012-04-09 15:55:49
那么你的解决方案提供的问题是好的 – Kobe 2012-04-09 15:58:22