|
From: Ioannis V. <no...@ya...> - 2001-09-05 22:33:01
|
> -----Original Message-----
> From: Michael C [mailto:jd...@ho...]=20
> Sent: Wednesday, September 05, 2001 11:40 PM
> To: Ioannis Vranos; Dev-C++
> Subject: Re: [Dev-C++] C++ question
>=20
>=20
> O.K. for argument sake I agree with you.
> I agree that the catch block is the end of the constructor=20
> only when it is present.
>=20
> What I would like to know is what happens when you have an=20
> initialiser list of two or more objects that you want to=20
> place a try against.
>=20
> Would you have the following ?
>=20
> X () try : a(1), b(2), c(3)
> {
> ...
> }
>=20
> How would you determine in the catch who threw the exception=20
> ? I guess each object could throw a different kind of=20
> exception and there could be a catch for each kind. But if=20
> they all throw the same kind of exception I guess you would=20
> need some sort of identification in that exception to=20
> correlate it to the offending object.
Of course. or you can use inheritance and polymorphism to exception
types, like the following style:
class Except
{
public: virtual void showdata()=3D0;
};
class Error1: public Except
{
void showdata() { std::cout<<"Error of type 1\n; }
};
class Error2: public Except
{
void showdata() { std::cout<<"Error of type 2\n"; }
};
// ...
catch(Except &e)
{
e.showdata();
}
Stroustrup has all these in the same chapter. For abstracting system
dependencies you can use multiple inheritance.
>=20
> As I am using an older compiler, HPUX 10.20, I cannot play=20
> with this myself. When I move to HPUX 11, I'll be able to try it out.
You don't have Winblowz with Dev-C++? Why you don;t download the latest
gcc at least. 3.01 is excellent.
>=20
> Have you tried the above ?
yes your code with the 3 objects compiles too (provided that constructor
X() is modified to accept 3 arguments).
>=20
> Michael
Now a surprise. this thing doesn't apply only to constructors but to all
kinds of functions!
See the code (it compiles):
void f()
try
{
1+1;
}
catch(...)
{
}
int main()
{}
I saw it in next page of Special Edition (374). In that page see the
first 2 code fragments (that means that the following days i shall see
much Light). :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
|