|
From: Ioannis V. <no...@ya...> - 2001-09-05 03:23:23
|
If anyone knows, please answer.
=20
The question is about exception handling caught by a constructor. Here
is the code:
=20
class X
{
Vector v;
// ...
=20
public:
X(int);
// ...
};
=20
X::X(int s) try :v(s) // initialize v by s
{
// ...
}
=20
catch(Vector::Size)
{
// exceptions thrown for v are caught here
=20
//...
}
=20
------------------------------------------------------------------------
--------------------------------------------
=20
This is from the bible. The question here is, the constructor's body
ends at the end of catch body? if not, catch() belongs to where.
=20
This style compiles only for constructors defined outside of their
class. For the non-believers code that compiles:
=20
#include <iostream>
=20
class Exception {};
=20
class Vector
{
int size;
public:
Vector(int s):size(s) {}
};
=20
class X
{
Vector v;
public:
X(int);
=20
};
=20
X::X(int s) try :v(s)
{
std::cout<<"code run!\n";
=20
throw Exception();
=20
}
=20
catch(Exception)
{
std::cout<<"Exception caught!\n";
=20
=20
}
=20
int main()
{
X a(7);
}
=20
------------------------------------------------------------------------
--------------------------------------------
=20
=20
=20
Regards,
Ioannis
=20
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
<http://www.noicys.f2s.com/>=20
* Alternative URL: http://run.to/noicys
=20
|