【发布时间】:2016-02-22 20:49:48
【问题描述】:
我在exception.thrift 中定义了许多不同的异常:
exception InvalidArgumentsError {
1: string parameter
}
/**
* Server has an problem while executing a function.
* Execution aborted.
*/
exception ServerInternalError {
1: string parameter
}
/**
* Server answer is empty.
*/
exception NoDataError {
1: string parameter
}
这就是我在 C++ 代码中捕获它们的方式:
catch (InvalidArgumentsError & ex) {
std::cout << ex.parameter;
}
catch (ServerInternalError & ex) {
std::cout << ex.parameter;
}
catch (NoDataError & ex) {
std::cout << ex.parameter;
}
catch (apache::thrift::TException& ex) {
std::cout << "TException:\n" << ex.what();
} catch (const std::exception& ex) {
std::cout << ex.what();
return;
}
我想写这样的东西并捕获我所有的异常:
catch (SomeBasicException& ex) {
std::cout << ex.what();
}
catch (const std::exception& ex) {
std::cout << ex.what();
}
如果我只是捕获 TException 并调用 what() 我只会收到“默认 TException”消息,因为派生类不会覆盖虚拟 what() 方法。
thrift 编译器生成的代码:
class InvalidArgumentsError : public ::apache::thrift::TException {
public:
static const char* ascii_fingerprint; //..
static const uint8_t binary_fingerprint[16]; //..
InvalidArgumentsError() : parameter() {
}
virtual ~InvalidArgumentsError() throw() {}
std::string parameter;
_InvalidArgumentsError__isset __isset;
void __set_parameter(const std::string& val) {
parameter = val;
}
//...
};
【问题讨论】:
-
为什么要这样写?只是为了停止重复输入?
-
@Simple 是的,我有很多例外
-
in 0.9.3
what()应该在生成的 TException 子类中被覆盖