yohhoyの日記

技術的メモをしていきたい日記

オペランド指定無しthrow式

プログラミング言語C++のオペランド指定無しthrow式の振る舞いについてメモ。

#include <iostream>

void do_rethrow() {
  // オペランドなしthrow式
  throw;
}

int main()
{
  try {
    try {
      throw 42;
    } catch (...) {
      // int型の値42を再throwする
      do_rethrow();
    }
  } catch (int x) {
    std::cout << x << std::endl;
  }

  // std::terminate()によりプログラムが終了
  do_rethrow();
}

C++03 15.1/p6, 8より一部引用。

6 A throw-expression with no operand rethrows the exception being handled. (snip)

8 If no exception is presently being handled, executing a throw-expression with no operand calls terminate() (15.5.1).