GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::domain_error - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. stdexcept
  3. domain_error

最終更新日時: 2026年09月08日 15時55分33秒 (JST)
Akira Takahashi が更新

履歴 編集

class
<stdexcept>

std::domain_error

namespace std {
  class domain_error : public logic_error;
}

概要

関数引数に定義域の外の値が指定された場合に処理系が送出する例外およびその基底クラスである。 実際には標準ライブラリのコンポーネントはこの例外を送出せず、 代わりにユーザーが使うことができる。

メンバ関数

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

例

#include <iostream>
#include <cmath>
#include <stdexcept>

double calc_sqrt(double num) {
  if (num < 0) {
    throw std::domain_error("Cannot calculate the square root of a negative number.");
  }
  return std::sqrt(num);
}

int main() {
  try {
    std::cout << calc_sqrt(-4.0) << "\n";
  } catch (const std::domain_error& e) {
    std::cerr << e.what() << "\n";
  }
}

出力例 (標準エラー出力)

Cannot calculate the square root of a negative number.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.