length_error 类

此类用作引发报告尝试生成对象太长而难以指定的所有异常的基类。

语法

class length_error : public logic_error {
public:
    explicit length_error(const string& message);

    explicit length_error(const char *message);

};

备注

what() 返回的值是 message.data() 的副本。 有关详细信息,请参阅 whatdata

示例

// length_error.cpp
// compile with: /EHsc
#include <cstddef>
#include <exception>
#include <iostream>
#include <typeinfo>
#include <vector>
using namespace std;

int main()
{
   try
   {
      vector<int> v(100 + static_cast<size_t>(-1) / sizeof(int));
   }
   catch (const exception& e)
   {
      cerr << "Caught: " << e.what() << endl;
      cerr << "Type: " << typeid(e).name() << endl;
   }
}
/* Output:
Caught: vector too long
Type: class std::length_error
*/

要求

标头:<stdexcept>

命名空间: std

另请参阅

logic_error 类
C++ 标准库中的线程安全