Google's Go & Exceptions

dsimcha dsimcha at yahoo.com
Tue Jan 26 15:01:10 PST 2010


== Quote from Walter Bright (newshound1 at digitalmars.com)'s article
> Justin Johansson wrote:
> > (1) For some reason (possibly valid only in an historic context), I have
> > this great aversion to throwing exceptions from inside C++ constructors.
> >  From memory, I once threw an exception from inside a constructor
> > with an early C++ compiler and wound up with stack corruption or
> > something like that, and consequently I developed the practice of
> > forever more avoiding throwing from inside a C++ constructor.
> I'm a believer in the methodology that a constructor should be "trivial"
> in that it cannot fail (i.e. cannot throw). I'm probably in the minority
> with that view, but you shouldn't feel like you're doing the wrong thing
> when you stick to such a style.

I've never understood the recommendation not to put complex logic in the
constructor.  If you need complex logic to establish the class's invariant, or
need to initialize complicated immutable data structures, where else is that logic
supposed to go?



More information about the Digitalmars-d mailing list