我刚刚在我们的代码中修复了一个由异常切片引起的非常微妙的错误,现在我想确保我完全理解正在发生的事情。
这是我们的基本异常类、派生类和相关函数:
class Exception
{
public:
// construction
Exception(int code, const char* format="", ...);
virtual ~Exception(void);
<snip - get/set routines and print function>
protected:
private:
int mCode; // thrower sets this
char mMessage[Exception::MessageLen]; // thrower says this FIXME: use String
};
class Derived : public Exception {
public:
Derived (const char* throwerSays) : Exception(1, throwerSays) {};
};
void innercall {
<do stuff>
throw Derived("Bad things happened!");
}
void outercall {
try {
innercall();
}
catch(Exception& e)
{
printf("Exception seen here! %s %d\n", __FILE__, __LINE__);
throw e;
}
}
错误当然是外部调用最终抛出异常,而不是派生。我的错误是由于调用堆栈中较高的尝试捕获 Derived 失败造成的。
现在,我只是想确保我理解 - 我相信在“throw e”行,正在使用默认的复制构造函数创建一个新的异常对象。真的是这样吗?
如果是这样,我是否可以为将被抛出的对象锁定复制构造函数?我真的希望这种情况不再发生,并且我们的代码没有理由复制 Exception 对象(我知道)。
请不要评论我们有自己的异常层次结构这一事实。这是我正在努力纠正的一些旧设计(我正在取得良好进展。我已经摆脱了本土字符串类和许多本土容器。)
更新:需要明确的是,在我提出问题之前,我已经修复了这个错误(通过将“throw e”更改为“throw”)。我只是想确认发生了什么。