c++中异常处理中:logic_error、runtime_error

本文转载自:https://blog.youkuaiyun.com/hongqiang200/article/details/57955338

C++语言本身或标准程序库所抛出的所有异常,都派生自基类exception。这是其他数个标准异常类别的基类,它们共同构成一个类体系:

请输入图片描述

这些标准异常类别分为三组:

(1)语言本身所支持的异常

此类异常用以支撑某些语言特性。主要包括:
bad_alloc:new操作失败会抛出。
bad_cast:执行期间加在一个引用上面的动态性型别转换操作失败时抛出。
bad_typeid:执行RTTI时,交给typeid的参数为零或空指针时抛出
bad_exception:非预期的异常

(2)C++标准程序库发出的异常

总是派生自logic_error逻辑错误是由于程序内部逻辑而导致的错误。逻辑错误是可以避免的,且在程序开始执行之前,能够被检测到
C++标准库中定义的逻辑错误如下:

class logic_error : public exception {
   public:
      explicit logic_error (const string& what_arg);
};

class invalid_argument : public logic_error {
   public:
      explicit invalid_argument (const string& what_arg);
};

class out_of_range : public logic_error {
  public:
      explicit out_of_range (const string& what_arg);
};

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

class domain_error : public logic_error {
  public:
      explicit domain_error (const string& what_arg);
};

错误分类解释及举例:

domain_error:专业领域内的范畴 invalid_argument:无效参数,比如讲bitset以char而非0或1进行初始化 length_error:可能超越了最大极限,比如对着某个字符串附加太多字符。 out_of_range:参数不再预期范围内。例如在诸如array的容器或字符串string中采用一个错误索引。

(3)程序作用域之外发出的异常

总是派生自runtime_error,用来指出“不在程序范围内,且不容易回避”的事件。此类错误只在程序执行时才是可检测的。C++标准库中的定义如下:

class runtime_error : public exception {
    public:
        explicit runtime_error (const string& what_arg);
};

class range_error : public runtime_error {
    public:
        explicit range_error (const string& what_arg);
};
class overflow_error : public runtime_error {
    public:
        explicit overflow_error (const string& what_arg);
};

class underflow_error : public runtime_error {
    public:
        explicit underflow_error (const string& what_arg);
};

range_error:内部计算时发生区间错误

overflow_error:算数运算时发生上溢

underflow_error:算数运算时发生下溢

Update:
实例代码:

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

using namespace std;

//自定义配置器,vector分配空间使用
template<class _Ty>
class stingyallocator : public allocator<_Ty>
{
public:
   template <class U>
    struct rebind {
          typedef stingyallocator<U> other;
    };

   size_t max_size( ) const
   {
         return 10;
   };
};

int main()
{
    //逻辑错误:out_of_range
    try {
      string str( "Micro" );
      string rstr( "soft" );
      str.append( rstr, 5, 3 );
      cout << str << endl;
   }
   catch ( exception &e ) {
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid( e ).name( ) << endl << endl;
   };

   //逻辑错误:length_error
   try
   {
      vector<int, stingyallocator< int > > myv;
      for ( int i = 0; i < 11; i++ )
        myv.push_back( i );
   }
   catch ( exception &e )
   {
      cerr << "Caught " << e.what( ) << endl;
      cerr << "Type " << typeid( e ).name( ) << endl << endl;
   };

   //逻辑错误:invalid_argument
   try
   {
      bitset< 32 > bitset( string( "11001010101100001b100101010110000") );
   }
   catch ( exception &e )
   {
      cerr << "Caught " << e.what( ) << endl;
      cerr << "Type " << typeid( e ).name( ) << endl << endl;
   };

   //逻辑错误:domain_error
   try
   {
      throw domain_error( "Your domain is in error!" );
   }
   catch (exception &e)
   {
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid(e).name( ) << endl << endl;
   };

   //运行时错误:range_error
    try
   {
      throw range_error( "The range is in error!" );
   }
   catch (exception &e)
   {
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid( e ).name( ) << endl << endl << endl;
   };

   //运行时错误:underflow_error
   try
   {
      throw underflow_error( "The number's a bit small, captain!" );
   }
   catch ( exception &e ) {
      cerr << "Caught: " << e.what( ) << endl;
      cerr << "Type: " << typeid( e ).name( ) << endl << endl;
   };

    //运行时错误:overflow_error
    try
    {
        bitset< 33 > bitset;
        bitset[32] = 1;
        bitset[0] = 1;
        unsigned long x = bitset.to_ulong( );
    }
    catch(exception &e)
    {
        cerr << "Caught " << e.what() << endl;
        cerr << "Type: " << typeid(e).name() << endl << endl;
    }

    return 0;
}

运行结果:

点击查看原始尺寸

 

<think>根据用户的问题,用户想查找C++中std::runtime_error的头文件。 根据引用内容,我们可以在引用中看到关于std::runtime_error的使用示例,其中包含了头文件信息。 在引用[1]中,示例代码包含了头文件`<stdexcept>`,因为`std::runtime_error`定义在这个头文件中。 同样,引用[2]和引用[3]也提到了std::runtime_errorC++标准库中的异常类,属于`<stdexcept>`头文件。 因此,我们可以确定std::runtime_error的头文件是`<stdexcept>`。 回答结构: 1. 直接给出头文件名称。 2. 简要说明如何使用(包括示例代码)。 3. 生成相关问题。 注意:按照要求,行内数学表达式使用$...$,但本问题不涉及数学公式,所以不需要。独立公式也不需要。 使用中文回答。 生成相关问题。 在引用部分,我们引用了站内引用,所以需要在末尾添加引用标识。 由于用户的问题非常直接,我们直接给出答案。</think>### C++ std::runtime_error 头文件 在 C++ 中,`std::runtime_error` 异常类的头文件是 **`<stdexcept>`**。这个头文件包含在 C++ 标准库中,专门定义了标准异常类(如 `runtime_error`、`logic_error` 等)。 #### 基本用法: ```cpp #include <stdexcept> // 必需的头文件 void risky_operation() { if (/* 错误条件 */) { throw std::runtime_error("描述错误的具体原因"); } } int main() { try { risky_operation(); } catch (const std::runtime_error& e) { // 通过 e.what() 获取错误信息 } return 0; } ``` #### 关键说明: 1. **头文件作用**:`<stdexcept>` 定义了 `std::runtime_error` 及其派生类(如 `std::overflow_error`)[^3]。 2. **错误类型**:用于表示运行时错误(如文件打开失败、无效输入等非逻辑错误)[^3]。 3. **错误信息**:构造函数接受字符串参数(如 `throw std::runtime_error("文件未找到")`),可通过 `e.what()` 获取[^1][^2]。 > 示例:文件操作中的典型用法(需额外包含 `<fstream>`): > ```cpp > #include <stdexcept> > #include <fstream> > > void read_file(const std::string& filename) { > std::ifstream file(filename); > if (!file) throw std::runtime_error("打开文件失败: " + filename); > // ... > } > ``` --- ### 相关问题 1. `std::runtime_error` 和 `std::exception` 有什么区别? 2. 如何自定义继承自 `std::runtime_error` 的异常类? 3. 在哪些场景下应该优先使用 `std::runtime_error` 而非其他异常类型? [^1]: C++异常处理 std::runtime_error 使用详解 [^2]: C++中的std::runtime_error [^3]: std::runtime_error异常的解决方案
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值