Google's Go & Exceptions

Walter Bright newshound1 at digitalmars.com
Tue Jan 26 14:47:46 PST 2010


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.

C++ constructors throwing should work according to the C++ standard.

> (2) Also, and this might be urban myth, I'm of the belief that a try
> block does not come for free at runtime even if an exception is not 
> thrown (i.e. there's a cost to establishing the try block itself),
> that for performance critical code the old fashion return and check
> error code idiom is faster.

With 32 bit Windows, there is a small penalty even when not throwing. 
With Linux and 64 bit Windows, there is no penalty.

dmd will attempt to optimize away try blocks if the statements in them 
are all nothrow.



More information about the Digitalmars-d mailing list