C++ 标准库中的异常

来源:互联网 发布:瓦赫宁根大学 知乎 编辑:程序博客网 时间:2024/06/05 01:10

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。程序最后要回到构造异常类的执行点,析构异常类。
如果捕获异常没有采用按应用调用,还要考虑通过子类构造一个父类的拷贝构造过程。
最后,异常调用栈退栈完成,调用才结束。