function-try-block
function-try-block - особая более удобная форма записи try-блоков в функциях. Стандарт : 15[except], A.13[gram.except] (n3290)
function-try-block: try ctor-initializeropt compound-statement handler-seq
ctor-initializer - список инициализации (в конструкторе), опционально compound-statement - обычный блок { операции; } handler-seq - обработчик вида catch(exception& e) { …}
function-try-block работают для функций(gcc 4.5.1):
void foo()
try
{
throw "fsd";
}
catch(...)
{
std::cout << "Exception!";
return;
}
И этот код полностью эквивалентен обычно коду с исключениями:
void foo()
{
try
{
throw "fsd";
}
catch(...)
{
std::cout << "Exception!";
return;
}
}
Подобное работает и для конструкторов(gcc 4.5.1) :
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
}
Однако не заработало для деструкторов (gcc 4.5.1) :
struct a
{
a()
try : i(f()) {}
catch(int) {}
int i;
int f() { /*throw 1;*/ return 0; }
~a()
try
{
throw "hi!";
}
catch
{
cout << "Destructor thrown!";
}
};