std::basic_ios<CharT,Traits>::exceptions
來自 cppreference.com
std::ios_base::iostate exceptions() const; |
(1) | |
void exceptions( std::ios_base::iostate except ); |
(2) | |
獲取並設定流的異常掩碼。異常掩碼決定了哪些錯誤狀態會觸發 failure 型別的異常。
1) 返回異常掩碼。
2) 將異常掩碼設定為 except。如果流在呼叫時具有異常掩碼所覆蓋的錯誤狀態,則會立即觸發異常。
目錄 |
[編輯] 引數
異常規範 | - | 異常掩碼 |
[編輯] 返回值
1) 當前異常掩碼。
2) (無)
[編輯] 注意
本節不完整 原因:討論 LWG2349 並從 ios_base::clear 以及(非)格式化(輸入/輸出)函式需求頁面連結(或者行為應該在需求頁面上詳細闡述並從此處連結)。另請參閱 stackoverflow.com/a/35089910 |
[編輯] 示例
執行此程式碼
#include <fstream> #include <iostream> int main() { int ivalue; try { std::ifstream in("in.txt"); in.exceptions(std::ifstream::failbit); // may throw in >> ivalue; // may throw } catch (const std::ios_base::failure& fail) { // handle exception here std::cout << fail.what() << '\n'; } }
可能的輸出
basic_ios::clear: iostream error