C++ 异常库 - invalid_argument



描述

这是一个无效参数异常,标准库的某些组件也会抛出此类型的异常以指示无效参数。

声明

以下是 std::invalid_argument 的声明。

class invalid_argument;

C++11

class invalid_argument;

参数

返回值

异常

无异常保证 - 没有成员抛出异常。

成员

  • 构造函数 - what_arg 与成员 what 返回的值内容相同。

  • what − 用于获取标识异常的字符串。

示例

下面的示例解释了 std::invalid_argument。

#include <iostream>       
#include <stdexcept>      
#include <bitset>         
#include <string>         

int main (void) {
   try {    
      std::bitset<5> mybitset (std::string("9848011223"));
   }
   catch (const std::invalid_argument& ia) {
      std::cerr << "Invalid argument: " << ia.what() << '\n';
   }
   return 0;
}

让我们编译并运行上述程序,这将产生以下结果:

Invalid argument: bitset::_M_copy_from_ptr
exception.htm
广告
© . All rights reserved.