这个语法是什么意思? [英] What is the meaning of this syntax?
问题描述
我在 SO 问题中看到了特殊的语法 不久前.
class B{一个;民众:B() 尝试 : a() {} catch(string& s) { cout <<&s <<" " <<<<结束;};};
函数外的这个try-catch-block是什么意思?
是函数 try 块.仅在 c-tor 中用于在派生类构造函数中捕获错误.您可以在标准中阅读有关此功能的更多信息,例如 n3337 草案标准.15、15.1.
<块引用>4 function-try-block 将 handler-seq 与ctor-initializer(如果存在)和复合语句.一个例外在复合语句执行期间抛出,或者,对于构造函数和析构函数,在初始化或分别销毁类的子对象,转移以与函数相同的方式控制到函数尝试块中的处理程序执行 try 块期间抛出的异常转移控制给其他处理程序.[示例:
int f(int);C类{国际我;双d;民众:C(整数,双);};C::C(int ii, double id)尝试:i(f(ii)),d(id){//构造函数语句}抓住 (...) {//处理从构造函数初始化器抛出的异常//和来自构造函数语句}
<块引用>
——结束示例 ]
I have seen the peculiar syntax in an SO question a while ago.
class B{
A a;
public:
B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; };
};
What is the meaning of this try-catch-block outside the function?
It's function try block. Usefull only in c-tors for catch errors in derived classes constructors. You can read more about this feature in standard for example n3337 draft par. 15, 15.1.
4 A function-try-block associates a handler-seq with the ctor-initializer, if present, and the compound-statement. An exception thrown during the execution of the compound-statement or, for constructors and destructors, during the initialization or destruction, respectively, of the class’s subobjects, transfers control to a handler in a function-try-block in the same way as an exception thrown during the execution of a try-block transfers control to other handlers. [ Example:
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
}
—end example ]
这篇关于这个语法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!