我之前在一个SO问题中看到了一种奇特的语法。
class B{
A a;
public:
B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; };
};
这个try-catch块在函数外部有什么意义?
我之前在一个SO问题中看到了一种奇特的语法。
class B{
A a;
public:
B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; };
};
4个函数try块将处理程序序列与构造函数初始化器(如果存在)和复合语句相关联。在执行复合语句期间或对于构造函数和析构函数,在类的子对象的初始化或销毁期间抛出异常,将控制转移到同一函数try块中的处理程序,就像在执行try块期间抛出异常将控制转移给其他处理程序一样。 [示例:
int f(int);
class C {
int i;
double d;
public:
C(int, double);
};
C::C(int ii, double id)
try : i(f(ii)), d(id) {
// constructor statements
}
catch (...) {
// handles exceptions thrown from the ctor-initializer
// and from the constructor statements
}
—示例结束 ]