关于异常处理(需要的话可再加100分)
fsht 2002-05-12 08:41:58 近来学异常处理,不甚明了,请问下面的程序是不是对的?
#include <iostream>
class OutOfBounds {
public:
OutOfBounds() { std::cout << "Out of bounds"; }
};
class X {
public:
X();
~X() {}
int find(const int& index);
private:
enum { N = 20 };
int a[N];
};
X::X()
{
for (int i = 0; i < N; i++)
a[i] = i;
}
int X::find(const int& index)
{
if (index >= N || index < 0)
throw OutOfBounds();
return a[index - 1];
}
main() {}