...ing logging 4.0

はてなブログに移行しました。D言語の話とかいろいろ。

function-try-block

ええええこんなのあったのかああああ.

#include <iostream>

class A
{
public:
	int value;
	A() try : value(0) // 初期化リストは普通だがtry文までもが!?
	{
		std::cout << "throw exception" << std::endl;
		throw "fire";
	}
	catch (...)
	{
		std::cout << "catch exception" << std::endl;
		// コンストラクタまたはデストラクタの場合は
		// 例外が再送出される
	}
};

// もちろんmain関数でもこう書ける
int main() try
{
	A a;
	return 0;
}
catch (...)
{
	// 再送出されるのでここでもcatchされます
	std::cout << "catch exception" << std::endl;
}