|
From: Ioannis V. <no...@ya...> - 2001-09-05 14:13:34
|
I know all that. My question was if tthe body of the constructor ended at the end of the catch() block or at the end of the try block. I assume the end of catch() block so far. Warm regards, Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: Michael Codadeen [mailto:jd...@ho...]=20 > Sent: Wednesday, September 05, 2001 9:59 AM > To: no...@ya...; dev...@li... > Subject: Re: [Dev-C++] C++ question >=20 >=20 > Hi, >=20 > I've just been speaking to some grus and they tell me this is=20 > a new feature=20 > in the standards to catch exceptions in constructors. >=20 > Before constructors are invoked the variables of the class=20 > are initialised.=20 > If any throw an exception they are not caught. With this new method=20 > exceptions are caught. >=20 > This is not entirely true as you can have pointers=20 > initialised to 0, and=20 > then within the braces have a try/catch to assign something to them.=20 > However, this feature is to be used with the initialiser list. >=20 > I suppose this aids 'resource allocation is initialisation'=20 > that Strousroup=20 > talks about (i.e. auto pointers). As you know, in this case=20 > resources are=20 > able to be freed if a class constructor throws an exception=20 > on construction=20 > and doesn't catch it. >=20 > Hope this helps, >=20 > Michael >=20 >=20 > >From: "Ioannis Vranos" <no...@ya...> > >To: "Dev-C++" <dev...@li...> > >Subject: [Dev-C++] C++ question > >Date: Wed, 5 Sep 2001 06:24:36 +0200 > > > >If anyone knows, please answer. > > > >The question is about exception handling caught by a=20 > constructor. Here=20 > >is the code: > > > >class X > >{ > >Vector v; > >// ... > > > >public: > >X(int); > >// ... > >}; > > > >X::X(int s) try :v(s) // initialize v by s > >{ > >// ... > >} > > > >catch(Vector::Size) > >{ > >// exceptions thrown for v are caught here > > > >//... > >} > > > >------------------------------------------------------------- > ---------- > >- > >-------------------------------------------- > > > >This is from the bible. The question here is, the constructor's body=20 > >ends at the end of catch body? if not, catch() belongs to where. > > > >This style compiles only for constructors defined outside of their=20 > >class. For the non-believers code that compiles: > > > >#include <iostream> > > > >class Exception {}; > > > >class Vector > >{ > >int size; > >public: > >Vector(int s):size(s) {} > >}; > > > >class X > >{ > >Vector v; > >public: > >X(int); > > > >}; > > > >X::X(int s) try :v(s) > >{ > >std::cout<<"code run!\n"; > > > >throw Exception(); > > > >} > > > > catch(Exception) > > { > > std::cout<<"Exception caught!\n"; > > > > > > } > > > > > >int main() > >{ > >X a(7); > >} > > > >------------------------------------------------------------- > ---------- > >- > >-------------------------------------------- > > > > > > > >Regards, > >Ioannis > > > >* Ioannis Vranos > >* Programming pages: http://www.noicys.f2s.com=20 > ><http://www.noicys.f2s.com/> > >* Alternative URL: http://run.to/noicys > > >=20 >=20 > _________________________________________________________________ > Get your FREE download of MSN Explorer at=20 > http://explorer.msn.com/intl.asp >=20 |