|
From: Ioannis V. <no...@ho...> - 2002-06-22 10:22:51
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Johnny Dess=B5
> Sent: Saturday, June 22, 2002 1:00 AM
> To: Dev-Cpp
> Subject: [Dev-C++] Help
>=20
>=20
> C++ Exceptions are not catched!!!!!!!!!!!!!!!
>=20
> Why?
>=20
> I had build a Dll who throws some exception but the program=20
> 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)
>=20
>=20
> #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 <=3D 0) throw BadSize();
> si =3D s;
> value =3D new int[si];
> if (value =3D=3D 0) throw BadAllocation();
> }
> int& Vector::operator[] (size_t i)
> {
> if ((i < 0) || (i >=3D 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 =3D 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);
> }
>=20
> )
> (
> Executing: C:\Program Files\ConTEXT\ConExec.exe "g++" "temp.cpp" -o
temp -ansi -pedantic-errors -Wall -fexpensive-optimizations -O3
-ffloat-store -mcpu=3Dpentiumpro
In file included from c:/mingw/include/g++-v3/backward/iostream.h:31,
from temp.cpp:2:
c:/mingw/include/g++-v3/backward/backward_warning.h:32:2: warning:
#warning This file includes at least one deprecated or antiquated
header. Please consider using one of the 32 headers found in section
17.4.1.2 of the C++ standard. Examples include substituting the <X>
header for the <X.h> header for C++ includes, or <sstream> instead of
the deprecated header <strstream.h>. To disable this warning use
-Wno-deprecated.
> Execution finished.
C:\c>temp
Give the size of the array: 0
The size of an array must be greater than 0.
I think it works ok to me. But you use the old style standard C++
headers which is deprecated (are not developed anymore in the latest
versions of compilers, and are kept for backward compatibility only).
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|