Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 2.24 KB

File metadata and controls

71 lines (54 loc) · 2.24 KB

overflow_error

  • stdexcept[meta header]
  • std[meta namespace]
  • class[meta id-type]
namespace std {
  class overflow_error : public runtime_error;
}
  • runtime_error[link runtime_error.md]

概要

overflow_errorクラスは、算術演算の結果がオーバーフロー(扱える値の上限を超過)したことを通知するために送出される例外クラスである。

標準ライブラリでは、std::bitsetto_ulong()to_ullong()が、値を戻り値の型で表現できない場合にこの例外を送出する。

このクラスはruntime_errorを公開継承しており、runtime_errorとして捕捉できる。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ
(destructor) デストラクタ
operator= 代入演算子
what エラー理由を取得する(std::exceptionから継承)

#include <stdexcept>
#include <iostream>
#include <bitset>

int main()
{
  std::bitset<128> bits;
  bits.set(127);

  try {
    // 128ビットの値はunsigned longでは表現できない
    unsigned long value = bits.to_ulong();
    std::cout << value << std::endl;
  }
  catch (const std::overflow_error& e) {
    std::cout << "overflow_error: " << e.what() << std::endl;
  }
}
  • std::overflow_error[color ff0000]
  • bits.to_ulong()[link /reference/bitset/bitset/to_ulong.md]
  • bits.set[link /reference/bitset/bitset/set.md]

出力例

overflow_error: _Base_bitset::_M_do_to_ulong

例外オブジェクトが保持するメッセージの内容は、処理系によって異なる。

関連項目

参照