#include "stdafx.h"
#include <iostream.h>
class Vector
{
private:
int *p;
int m_nLen;
public:
Vector(int nLen)
{
p = new int [nLen];
m_nLen = nLen;
}
~Vector( )
{
delete [] p;
}
int size()
{
return m_nLen;
}
// 重载 []
int &operator[](int i)
{
if (i < 0 || i >= m_nLen)
{
//抛出一个类异常
throw Range();
}
return p[i];
}
//声明类::类
class Range
{
};
};
int main(int argc, char* argv[])
{
Vector theVec(2);
try
{
//会跳到下面的double分支中
//throw 10.2;
//这一句会引发一个异常
int i = theVec[2];
cout << i << endl;
}
catch (int)
{
}
catch (float)
{
}
catch (double)
{
}
//收到哪个类的异常
catch (Vector::Range&)
{
cout << "范围错误" << endl;
}
catch (...)
{
cout << "未知错误" << endl;
}
return 0;
}