【发布时间】:2017-01-12 15:54:25
【问题描述】:
我刚刚创建了一个异常层次结构,并希望我的 catch 块显示派生异常的消息。 我有 5 个这样的例外:
class ImagetypeException : public TGAException {
public:
const char* what() const throw();
};
const char* ImagetypeException::what() const throw() {
return "Der Bildtyp ist nicht \"RGB unkomprimiert\".";
}
它们都是从 TGAException 派生的,即从 std::exception 派生的。
class TGAException : public std::exception {
public:
virtual const char* what() const throw();
};
const char* TGAException::what() const throw() {
return "Beim Einlesen oder Verarbeiten der TGA-Datei ist ein unerwarteter Fehler aufgetreten!";
}
所以我显然想在我的代码中的某个时刻抛出这些,并认为这可能是一个好主意,以尽量减少我需要的捕获块的数量。
catch (TGAException e) {
cout << e.what() << endl;
}
如果我这样做,将打印的消息是来自 TGAException 的消息,但我希望它显示更具体的派生消息。 那么,我需要做什么才能让它按我想要的方式工作呢?
【问题讨论】:
-
通过引用捕获,如
catch (TGAException& e)。当你按价值捕捉时,你slice it -
更好的是,通过 const 引用捕获,因为您不打算更改异常对象。 @IgorTandetnik 你应该把这个作为答案(或者我会,但我不是想偷代表)
-
你正在做对象切片,直到你通过引用捕获。
-
其实
"Der Bildtyp ist nicht \"RGB unkomprimiert\"."好像和std::invalid_argument匹配,你确定需要自己的异常类吗?