Namespaces
Variants

std:: bad_exception

From cppreference.net
Utilities library
定義済みヘッダー <exception>
class bad_exception : public exception

std::bad_exception は、以下の状況でC++ランタイムによってスローされる例外の型です:

  • std::exception_ptr が捕捉された例外のコピーを格納しており、かつ std::current_exception によって捕捉された例外オブジェクトのコピーコンストラクタが例外をスローする場合、捕捉される例外は std::bad_exception のインスタンスとなる。
(C++11以降)
  • 動的例外仕様 が違反され、かつ std::unexpected が例外仕様に依然として違反する例外をスローまたは再スローするが、例外仕様が std::bad_exception を許可している場合、 std::bad_exception がスローされる。
(C++17まで)
cpp/error/exception std-bad exception-inheritance.svg

継承図

std::bad_exception のすべてのメンバー関数は constexpr です。

(C++26以降)

目次

メンバー関数

bad_exception オブジェクトを構築する
(public member function)
オブジェクトをコピーする
(public member function)
[virtual]
説明文字列を返す
(virtual public member function)

std::exception から継承 std:: exception

メンバ関数

[virtual]
例外オブジェクトを破棄
( std::exception の仮想公開メンバ関数)
[virtual]
説明文字列を返す
( std::exception の仮想公開メンバ関数)

注記

機能テスト マクロ 標準 機能
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 例外型のためのconstexpr

C++14またはそれ以前のモードでのみコンパイルされます(警告が発行される場合があります)。

#include <exception>
#include <iostream>
#include <stdexcept>
void my_unexp()
{
    throw;
}
void test()
    throw(std::bad_exception) // Dynamic exception specifications
                              // are deprecated in C++11
{
    throw std::runtime_error("test");
}
int main()
{
    std::set_unexpected(my_unexp); // Deprecated in C++11, removed in C++17
    try
    {
        test();
    }
    catch (const std::bad_exception& e)
    {
        std::cerr << "Caught " << e.what() << '\n';
    }
}

出力例:

Caught std::bad_exception