我该怎么做,
我有一个名为LargeInteger的类,它存储最多20位数。我创建了构造器
LargeInteger::LargeInteger(string number){ init(number); }现在,如果数字> LargeInteger::MAX_DIGITS (静态常量成员),即20,我不想创建对象并抛出异常。
我创建了一个类LargeIntegerException{ ... };并执行了以下操作
void init(string number) throw(LargeIntegerException);
void LargeInteger::init(string number) throw(LargeIntegerException)
{
if(number.length > MAX_DIGITS)
throw LargeIntegerException(LargeIntegerException::OUT_OF_BOUNDS);
else ......
}所以现在我修改了构造函数
LargeInteger::LargeInteger(string number)
{ try {init(number);} catch(LargeIntegerExceptione) {...} }现在我有两个问题
1.如果抛出异常,是否会创建此类的对象?
2.如果上述情况属实,该如何处理?
发布于 2012-07-28 09:04:36
没有理由在构造函数中捕获异常。您希望构造器失败,因此构造器外部的某些东西必须捕获它。如果构造函数通过异常退出,则不会创建任何对象。
LargeInteger(string num) { init(num); } // this is just fine.https://stackoverflow.com/questions/11697315
复制相似问题