<exception>

std::bad_exception

class bad_exception;
由 unexpected 處理器丟擲的異常

這是一種特殊的異常型別,專門用於列入函式的動態異常說明符(即其 throw 說明符)中。

如果一個函式在其動態異常說明符中列出了 bad_exception,但丟擲了未列出的異常,並且 unexpected 重新丟擲了該異常(或丟擲了任何其他也未包含在動態異常說明符中的異常),則會自動丟擲一個 bad_exception

其成員 what 返回一個空終止字元序列,用於標識該異常。

(自 C++11 起) 在 current_exception 的某些實現下,此異常會被丟擲,以指示覆制異常物件的操作失敗。

相容性

*dynamic-exception-specifier*的使用已被棄用(自C++11起)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// bad_exception example
#include <iostream>       // std::cerr
#include <exception>      // std::bad_exception, std::set_unexpected

void myunexpected () {
  std::cerr << "unexpected handler called\n";
  throw;
}

void myfunction () throw (int,std::bad_exception) {
  throw 'x'; // throws char (not in exception-specification)
}

int main (void) {
  std::set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { std::cerr << "caught int\n"; }
  catch (std::bad_exception be) { std::cerr << "caught bad_exception\n"; }
  catch (...) { std::cerr << "caught some other exception\n"; }
  return 0;
}

輸出

unexpected handler called
caught bad_exception


異常安全

無異常保證:無成員丟擲異常。

另見