|
From: Michael C. <jd...@ho...> - 2001-09-05 07:59:07
|
Hi,
I've just been speaking to some grus and they tell me this is a new feature
in the standards to catch exceptions in constructors.
Before constructors are invoked the variables of the class are initialised.
If any throw an exception they are not caught. With this new method
exceptions are caught.
This is not entirely true as you can have pointers initialised to 0, and
then within the braces have a try/catch to assign something to them.
However, this feature is to be used with the initialiser list.
I suppose this aids 'resource allocation is initialisation' that Strousroup
talks about (i.e. auto pointers). As you know, in this case resources are
able to be freed if a class constructor throws an exception on construction
and doesn't catch it.
Hope this helps,
Michael
>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 constructor. Here
>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
>ends at the end of catch body? if not, catch() belongs to where.
>
>This style compiles only for constructors defined outside of their
>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
><http://www.noicys.f2s.com/>
>* Alternative URL: http://run.to/noicys
>
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
|