|
From: <jd...@da...> - 2002-06-21 22:04:12
|
C++ Exceptions are not catched!!!!!!!!!!!!!!!
Why?
I had build a Dll who throws some exception but the program who calls dll
methods don't catch exceptions and closes abnormally.
Then I tried this simple exe: don't works at all.
Here the simple exe's code.
There's something wrong?
Please help me. (I use Dev 4.9.3.0)
#include <sys/types.h>
#include <iostream.h>
#include <stdlib.h>
class Vector
{
private:
size_t si;
int *value;
public:
class BadIndex {};
class BadSize {};
class BadAllocation {};
Vector (size_t);
int& operator[](size_t);
};
Vector::Vector (size_t s)
{
if (s <= 0) throw BadSize();
si = s;
value = new int[si];
if (value == 0) throw BadAllocation();
}
int& Vector::operator[] (size_t i)
{
if ((i < 0) || (i >= si)) throw BadIndex();
return value[i];
}
int main (int, char **)
{
Vector *v;
try
{
int si, index;
cout << "Give the size of the array: ";
cin >> si;
v = new Vector (si);
cout << "Give an index in the array: ";
cin >> index;
cout << "Give its value: ";
cin >> (*v)[index];
}
catch (Vector::BadSize)
{
cerr << "The size of an array must be greater than 0.\n";
exit(1);
}
catch (Vector::BadAllocation)
{
cerr << "Memory allocation error.\n";
exit(2);
}
catch (Vector::BadIndex)
{
cerr << "Index out of range.\n";
exit(3);
}
//...
system("pause");
exit(0);
}
)
(
[_] Johnny
|