<stdexcept>

std::invalid_argument

class invalid_argument;
無效引數異常

此類定義了用於報告無效引數的異常物件的型別。

它是程式可以丟擲的標準異常。標準庫的某些元件也丟擲此型別的異常來表示無效引數。

它定義為
1
2
3
4
class invalid_argument : public logic_error {
public:
  explicit invalid_argument (const string& what_arg);
};
1
2
3
4
5
class invalid_argument : public logic_error {
public:
  explicit invalid_argument (const string& what_arg);
  explicit invalid_argument (const char* what_arg);
};

成員

建構函式
傳遞給 what_arg 的字串與成員 what 返回的值內容相同。

此類從 logic_error 繼承了 what 成員函式。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// invalid_argument example
#include <iostream>       // std::cerr
#include <stdexcept>      // std::invalid_argument
#include <bitset>         // std::bitset
#include <string>         // std::string

int main (void) {
  try {
    // bitset constructor throws an invalid_argument if initialized
    // with a string containing characters other than 0 and 1
    std::bitset<5> mybitset (std::string("01234"));
  }
  catch (const std::invalid_argument& ia) {
	  std::cerr << "Invalid argument: " << ia.what() << '\n';
  }
  return 0;
}

可能的輸出

Invalid argument: bitset::_M_copy_from_string


異常安全

強保證: 如果建構函式丟擲異常,則沒有副作用。

另見