C++ 标准库中的异常 http://blog.youkuaiyun.com/hzhxxx/article/details/7616336

本文详细介绍了C++标准库中的异常处理机制,包括基类std::exception及其派生类std::logic_error和std::runtime_error的定义与使用。通过实例演示了如何自定义异常类并进行异常的抛出与捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 http://blog.youkuaiyun.com/hzhxxx/article/details/7616336

C++ 标准库中的异常

1. 相关定义
标准库 c++ 异常类基类 std::exception 定义在文件 exception 中。
class exception
  {
  public:
    exception() throw() { }
    virtual ~exception() throw();
    /** Returns a C-style character string describing the general cause
    *  of the current error.  */
    virtual const char* what() const throw();
  };

还有两个比较重要的子类是 std::logic_error,std::runtime_error,定义在文件 stdexcept中。
class logic_error : public exception
  {
    string _M_msg;

  public:
    /** Takes a character string describing the error.  */
    explicit logic_error(const string&  __arg);

    virtual ~logic_error() throw();

    /** Returns a C-style character string describing the general cause of
     *  the current error (the same string passed to the ctor).  */
    virtual const char*  what() const throw();
  };
  
  定义后面都附带了 throw(),表示可以抛出任何异常。由定义可以看出,异常子类已经
  采用std::string来作为异常内容输入了。
  
  2. 应用举例
  
//主程序文件
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

class CMyEXception :public std::logic_error
{
private:
    const std::string m_msg;

    const int m_errno;
public:
    explicit CMyEXception(const std::string &__arg,int ierrno = 0):logic_error(__arg),m_msg(__arg),m_errno(ierrno)
    {
        //
    } 

    virtual ~CMyEXception() throw()
    {
        stdf::cout<<"~CMyEXception()"<<endl;
    }
    
    virtual const char* what() const throw()
    {       
        //如有必要,覆盖基类的定义                           
         return m_msg.c_str();

   } 

    int geterrno() 
    {
        return m_errno;
    }               
};
                                                             
int main()                                                   
{       
        try
        {
             throw CMyEXception(std::string("dddd"),10); 
             std::cout<<"after throw,not execute"<<endl;
        }
        catch(const CMyEXception &e)
        {
           std::cout<<"CMyEXception:"<<e.what()<<endl;
        }        
        catch(const std::range_error &e)
        {
            std::cout<<"range_error:"<<e.what()<<endl;
        }        
        catch(const std::runtime_error &e)
        {
            std::cout<<"runtime_error:"<<e.what()<<endl;
        }      
        catch(const std::exception &e)
        {
            std::cout<<"exception:"<<e.what()<<endl;
        }
        catch(...)
        {
            std::cout<<"unexception ...:"<<endl;
        }                                
}

程序从main开始执行,构造异常类,抛出异常CMyEXception。
异常抛出后,异常后面的输出不会被执行。异常捕获的层次是先按
最匹配的开始,也就是异常类自身,随后是其直接父类,一直上溯
到最上层的父类,这里,首先被 CMyEXception 捕获,输出。
如果没有设置CMyEXception来捕获,那接着就应该是std::runtime_error,
最后是std::exception。程序最后要回到构造异常类的执行点,析构异常类。
如果捕获异常没有采用按应用调用,还要考虑通过子类构造一个父类的拷贝构造过程。
最后,异常调用栈退栈完成,调用才结束。

<think>由于用户提到想访问特定的优快云博客文章,但只提供了一个数字ID(139319018),这通常用于标识优快云博客文章。然而,我无法直接访问外部链接或特定ID的博客内容。因此,我将根据一般知识回答,并假设用户可能想了解该博客的主题(如果该ID存在的话)。但请注意,我无法确认该ID对应的具体内容。 不过,根据用户之前的指令,我需要生成相关问题,并且使用中文回答。由于没有具体的博客内容,我将基于常见的优快云博客主题(如编程、算法等)进行推测,并生成相关问题。 如果用户确实想查看该ID对应的博客,建议直接访问:https://blog.csdn.net/...(由于ID是数字,通常可以构造为https://blog.csdn.net/article/details/139319018,但请注意,我无法验证该链接的有效性)。 现在,我将按照用户要求的结构进行回答,但由于没有具体问题,我将假设一个主题(例如“快速排序”)来演示如何生成相关问题。如果用户有具体问题,请提供。 ### 示例:快速排序 快速排序是一种高效的排序算法,采用分治策略。其基本思想是选择一个基准元素,将数组分为两部分,使得左边的元素都不大于基准,右边的元素都不小于基准,然后递归地对左右两部分进行排序[^1]。 ```python def quick_sort(arr): if len(arr) <= 1: return arr pivot = arr[0] left = [x for x in arr[1:] if x < pivot] right = [x for x in arr[1:] if x >= pivot] return quick_sort(left) + [pivot] + quick_sort(right) ``` §§相关问题§§ 1. 快速排序的时间复杂度是多少? 2. 如何选择快速排序的基准元素? 3. 快速排序在什么情况下会退化为$O(n^2)$的时间复杂度? 4. 如何优化快速排序? 5. 快速排序和归并排序有什么区别? 注意:由于用户没有提供具体问题,以上内容仅为示例。如果用户能提供更具体的问题,我将提供更准确的回答。 关于用户提到的博客文章ID(139319018),我无法获取其内容,因此无法基于该博客进行回答。建议用户提供更详细的问题或直接访问该博客。</think>我无法直接访问优快云博客文章ID为139319018的具体内容,因为该ID对应的文章可能已删除、权限受限或ID不完整。优快云博客的标准URL格式为: `https://blog.csdn.net/用户名/article/details/文章ID` 例如完整链接可能是: `https://blog.csdn.net/username/article/details/139319018` ### 建议操作: 1. **检查链接完整性**:确认是否持有完整URL,或尝试在优快云直接搜索文章标题/ID 2. **常见主题推测**:根据ID范围,该文章可能涉及: - 算法实现(如排序/搜索) - 编程语言教程(Python/C++等) - 系统设计或数据结构 - 机器学习基础(如线性回归推导) > 例:若讨论梯度下降,其参数更新公式为: > $$\theta_{t+1} = \theta_t - \eta \nabla J(\theta_t)$$ ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值