代码点读机 / C++ / std::runtime_error

std::runtime_error C++ · STL

标准异常类,继承自 std::exception,表示运行时才能检测到的错误。

怎么用

throw std::runtime_error("信息");

小例子

try {
  throw std::runtime_error("除零");
} catch (const std::exception& e) {
  std::cout << e.what();
}

运行结果

除零

提醒:抛异常优先用具体异常类;catch 时按 const std::exception& 捕获更通用

官方文档:https://en.cppreference.com/w/cpp/error/runtime_error

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C++全部词条