现在的位置: 首页 > 综合 > 正文

C++中异常类的使用方法

2013年10月12日 ⁄ 综合 ⁄ 共 2930字 ⁄ 字号 评论关闭

C++有很多的标准异常类:

namespace std

{

    //exception派生

    class logic_error; //逻辑错误,在程序运行前可以检测出来

 

    //logic_error派生

    class domain_error; //违反了前置条件

    class invalid_argument; //指出函数的一个无效参数

    class length_error; //指出有一个超过类型size_t的最大可表现值长度的对象的企图

    class out_of_range; //参数越界

    class bad_cast; //在运行时类型识别中有一个无效的dynamic_cast表达式

    class bad_typeid; //报告在表达试typeid(*p)中有一个空指针p

   

    //exception派生

    class runtime_error; //运行时错误,仅在程序运行中检测到

   

    //runtime_error派生

    class range_error; //违反后置条件

    class overflow_error; //报告一个算术溢出

    class bad_alloc; //存储分配错误

}

     请注意观察上述类的层次结构,可以看出,标准异常都派生自一个公共的基类exception。基类包含必要的多态性函数提供异常描述,可以被重载。下面是exception类的原型:

class exception

{

public:

    exception() throw();

    exception(const exception& rhs) throw();

    exception& operator=(const exception& rhs) throw();

    virtual ~exception() throw();

    virtual const char *what() const throw();

};

    其中的一个重要函数为what(),它返回一个表示异常的字符串指针。

    标准库异常类定义在以下四个头文件中

    1、exception头文件:定义了最常见的标准异常类,其类名为exception。只通知异常的产生,但不会提供更多的信息

    2、stdexcept头文件定义了以下几种常见异常类

   函数                                                     功能或作用

exception                                                最常见的问题

runtime_error                                          运行时错误:仅在运行时才能检测到的问题

range_error                                             运行时错误:生成的结果超出了有意义的值域范围

overflow_error                                        运行时错误:计算上溢

underflow_error                                      运行时错误:计算下溢

 

logic_error                                                逻辑错误:可在运行前检测到的问题

domain_error                                          逻辑错误:参数的结果值不存在

invalid_argument                                    逻辑错误:不合适的参数

length_error                                             逻辑错误:试图生成一个超出该类型最大长度的对象

out_of_range                                            逻辑错误:使用一个超出有效范围的值

    3、new头文件定义了bad_alloc异常类型,提供因无法分配内存而由new抛出的异常

    4、type_info头文件定义了bad_cast异常类型(要使用type_info必须包含typeinfo头文件)

    下面是使用异常类的例子:

    首先,我定义了几个异常类,这些类也可以从标准异常类进行派生,如下

class BadInitializers
{
public:
 BadInitializers() {}
};

class OutOfBounds
{
public:
 OutOfBounds(int i) { cout<<"Size "<<i<<" is illegal!!!"<<endl; }
};

class SizeMismatch
{
public:
 SizeMismatch() {}
};

 

然后要在程序中需要的地方使用throw来抛出异常类,两个抛出异常类的例子如下

template <class T>
Array1D<T>::Array1D(int sz)
{
 if(sz<0)
 {
    //throw BadInitializers();

    throw invalid_argument("Size has to be bigger than 0!!!");
 }
 size=sz;
 element=new T[size];
}

template <class T>
T &Array1D<T>::operator[](int i) const
{
 if(i<0||i>=size)
 {
  throw OutOfBounds(i);
 }
 return element[i];
}

 

然后在主程序中使用try...catch...来捕获异常,并进行相应的处理,如下

try
 {
  int i=0;
  Array1D<int> a1(5);
  a1[0]=1;
  a1[1]=3;
  a1[2]=5;
  a1[3]=7;
  a1[4]=8;
  Array1D<int> a2(a1);
  for(i=0;i<a2.Size();i++)
  {
   cout<<a2[i]<<" ";
  }
  cout<<endl;

  Array1D<int> a3(5);
  a3=a1+a2;
  cout<<a3;
   }
 catch(BadInitializers)
 {
  cout<<"Error:BadInitializers!!!"<<endl;
 }
 catch(OutOfBounds &e)
 {
  cout<<"Error:OutOfBounds!!!"<<endl;
 }
 catch(SizeMismatch &e)
 {
  cout<<"Error:SizeMismatch!!!"<<endl;
 }

 catch(invalid_argument &e)
 {
  cout<<"Error:"<<e.what()<<endl;
 }
 catch(...)
 {
  cout<<"An unknown error!!!"<<endl;
 }

抱歉!评论已关闭.