名稱空間
變體
操作

std::bad_exception

來自 cppreference.com
< cpp‎ | 錯誤
 
 
 
 
 
定義於標頭檔案 <exception>
class bad_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/exceptionstd-bad exception-inheritance.svg

繼承圖

std::bad_exception 的所有成員函式都是 constexpr

(C++26 起)

目錄

[編輯] 成員函式

構造 bad_exception 物件
(公開成員函式)
複製物件
(公開成員函式)
[虛擬函式]
返回解釋字串
(虛公共成員函式)

繼承自 std::exception

成員函式

[虛擬函式]
銷燬異常物件
(std::exception 的虛公共成員函式) [編輯]
[虛擬函式]
返回解釋字串
(std::exception 的虛公共成員函式) [編輯]

[編輯] 注意

特性測試 標準 特性
__cpp_lib_constexpr_exceptions 202411L (C++26) 異常型別的 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