std::runtime_error

来自cppreference.com
< cpp‎ | error
在标头 <stdexcept> 定义
class runtime_error;

定义作为异常抛出的对象类型。它报告源于程序范围之外,且无法轻易预测到的错误。

std::runtime_error 的全部成员函数均为 constexpr:在常量表达式求值中创建并使用 std::runtime_error 对象是可能的。

然而,std::runtime_error 对象通常不能为 constexpr,因为任何动态分配的存储都必须在相同的常量表达式求值中释放。

(C++26 起)
cpp/error/exceptionstd-runtime error-inheritance.svg
关于这幅图像

继承图

下列标准库组件会抛出 std::runtime_error 类型异常:

(C++20 起)

另外,以下标准异常类型派生自 std::runtime_error

(C++11 起)
(C++17 起)
(C++20 起)

目录

[编辑] 成员函数

(构造函数)
构造拥有给定消息的新 runtime_error 对象
(公开成员函数)
operator=
替换 runtime_error 对象
(公开成员函数)

std::runtime_error::runtime_error

runtime_error( const std::string& what_arg );
(1) (C++26 起为 constexpr)
runtime_error( const char* what_arg );
(2) (C++26 起为 constexpr)
runtime_error( const runtime_error& other );
(3) (C++11 起为 noexcept)
(C++26 起为 constexpr)
1) 构造以 what_arg 作为解释字符串的异常对象。构造后 std::strcmp(what(), what_arg.c_str()) == 0
2) 构造以 what_arg 作为解释字符串的异常对象。构造后 std::strcmp(what(), what_arg) == 0
3) 复制构造函数。如果 *thisother 的动态类型都是 std::runtime_error,那么