namespace std {
int uncaught_exceptions() noexcept; // (1) C++17
}
概要
キャッチされていない例外の数を取得する。
戻り値
呼び出し時点の、そのスレッドにおいて送出されたがキャッチされていない例外オブジェクトの数を取得する。
具体的には、tryブロック中で作られたオブジェクトのデストラクタや、スタック巻き戻し(unwind)中のデストラクタで1以上になる。
例外
投げない
例
#include <iostream>
#include <exception>
struct CheckExcept {
~CheckExcept() {
std::cout << std::uncaught_exceptions() << std::endl;
}
};
struct ThrowAgain {
~ThrowAgain() {
try {
CheckExcept two{};
std::cout << "throw exception 2" << std::endl;
throw std::exception{};
} catch(...) {
std::cout << "catch exception 1" << std::endl;
}
}
};
int main() {
CheckExcept zero{};
try {
CheckExcept one{};
ThrowAgain two{};
std::cout << "throw exception 1" << std::endl;
throw std::exception{};
} catch(...) {
std::cout << "catch exception 2" << std::endl;
}
}
出力
throw exception 1
throw exception 2
2
catch exception 1
1
catch exception 2
0
バージョン
言語
- C++17
処理系
- Clang: 6 ✅
- GCC: 3.7 ✅
- Visual C++: 2015 ✅, 2017 ✅
関連項目
参照
- C++1z 現在発生している例外の数を取得する
uncaught_exceptions()関数 - Faith and Brave - C++で遊ぼう - N4152
uncaught_exceptions - N4259 Wording for
std::uncaught_exceptions - CWG 2098 Is
uncaught_exceptions()per-thread? - P3842R2 A conservative fix for constexpr
uncaught_exceptions()andcurrent_exception()- C++26の策定中に
constexprが追加されたが、本提案文書により巻き戻された (C++29で再検討予定)
- C++26の策定中に