Topic: exceptions in initialization lists


Author: comeau@panix.com (Greg Comeau)
Date: 1999/11/05
Raw View
In article <7vphvl$7av$1@trex.antw.online.be> "TiTi" <TJunkMeBigTime@mad.dog.com> writes:
>> how does one take care of execeptions thrown in the initialization lists?
>
>I once started a thread on 'comp.lang.c++', that ended up in discussing the
>things you know ask. The basic idea is to wrap the constructor (or was it
>just the initializer list?) in a try - catch clause. Check the thread
>"Initialization order." on www.deja.com (author: TiTi (that's me)), and the
>replies of course (I was pretty wrong in my post, but that doesn't matter).
>The reply from Greg Comeau was pretty cool (described the syntax). I don't
>know if this is in the standard though (maybe I didn't get that far yet).

The syntax I recall showing in that thread is Standard.
Standard is all I speak :)

Note: I have no idea which compilers do or don't implement
the syntax yet though (Comeau C++ does now FYI).

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- NOTE 4.2.42 BETAS NOW AVAILABLE
    Email: comeau@comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
                *** WEB: http://www.comeaucomputing.com ***
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "TiTi" <Serra.Angel@Serra.Shrine>
Date: 1999/11/08
Raw View
As I remember, you were the only one that could reproduce what was in the
standard. I know it is in the standard (I just finished reading it),
actually it's on the first/second page of clause 15 (p317-318).
I have full respect for walking talking C++ standard freax.

TiTi
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Anupam Kapoor <anupamk@mindspring.com>
Date: 1999/11/03
Raw View
hi all,

i understand that it is generally a good idea to initialize class
members in the initialization list (for const and reference members
it is the only way possible). i am sorry for a stupid(or one that has
alread been discussed) question, but how does one take care of
execeptions thrown in the initialization lists ? for example

 class X
 {
 private:
    const C create_c_() const;         // throws an exception
 private:
    const A a_;
    const B b_;
    const C c_;
 };

 X::X()
   : a_(A()), b_(B()),c_(create_c_())
    ^^^^^^^^^^^ boom !!!
 { }
if an exception occurs in ctor, the standard gurantees that destructors
will be called for all partially created objects. but what if i want to
do
something more here ? one possiblity is that i can wrap up, create_c_()
call in another method and do the exception handling there e.g.

 const C X::create_c_proxy_() const
 {
 try        { return this->create_c_(); }
 catch(...) { /* do something */ }
 }
but going through this elaborate mechanism for all variables is pretty
cumbersome and not-so readable imho.

is there a way of doing this ? if it is can someone please  indicate the
relevant section of the standard where it has been expostulated ? if not
what is the best way of handling this scenario ?

thanks

anupam
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/03
Raw View
Anupam Kapoor wrote:
>
> hi all,
>
> i understand that it is generally a good idea to initialize class
> members in the initialization list (for const and reference members
> it is the only way possible). i am sorry for a stupid(or one that has
> alread been discussed) question, but how does one take care of
> execeptions thrown in the initialization lists ? for example

By making the entire constructor function body a try block.

>
>         class X
>         {
>         private:
>            const C create_c_() const;         // throws an exception
>         private:
>            const A a_;
>            const B b_;
>            const C c_;
>         };
>
>         X::X()

Insert the following lines:

 try

>           : a_(A()), b_(B()),c_(create_c_())
>                                 ^^^^^^^^^^^ boom !!!
>         { }

and:

 catch(whatever)
 {
  // exception handler: includes exceptions thrown
  // by initializers.
 }

....
> is there a way of doing this ? if it is can someone please  indicate the
> relevant section of the standard where it has been expostulated ? if not

See section 15 p1 for the description of a function-try-block, and
section 8.4 p1 for how one fits into a function definition. There are
lots of special rules for the handling of function-try-blocks,
particularly in section 15 - search for them.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "TiTi" <TJunkMeBigTime@mad.dog.com>
Date: 1999/11/03
Raw View
I once started a thread on 'comp.lang.c++', that ended up in discussing the
things you know ask. The basic idea is to wrap the constructor (or was it
just the initializer list?) in a try - catch clause. Check the thread
"Initialization order." on www.deja.com (author: TiTi (that's me)), and the
replies of course (I was pretty wrong in my post, but that doesn't matter).
The reply from Greg Comeau was pretty cool (described the syntax). I don't
know if this is in the standard though (maybe I didn't get that far yet).


TiTi


> i understand that it is generally a good idea to initialize class
> members in the initialization list (for const and reference members
> it is the only way possible). i am sorry for a stupid(or one that has
> alread been discussed) question, but how does one take care of
> execeptions thrown in the initialization lists ? for example




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]