write(f)ln style exception factory

Dmitry Olshansky via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 15 02:40:11 PDT 2014


15-Jun-2014 02:03, Element 126 пишет:
> On 06/14/2014 09:04 PM, Dmitry Olshansky wrote:
>> Recalling the previous discussion of throwing exception being costly, I
>> thought the idiom of "pay as you go" is worth incorporating into the
>> standard library.
>>
>> In brief:
>>
>> throw exception("CPU temperature is below", 2.5, "K");
>> vs
>> throw new Exception(text("CPU temperature is below", 2.5, "K"));
>> or:
>> enforce(false, text("CPU temperature is below", 2.5, "K"));
>>
>> The benefit that the latter will only construct string if printing is
>> indeed requested.
>>
>> Proof of concept:
>> https://gist.github.com/DmitryOlshansky/59ec5953874bc1985ac5
>>
>> The problem with it is that for some reason writeln of exception won't
>> compile while thrown exception is printed just fine by the trace handler.
>>
>> Thoughts?
>>
>
> ---
> writeln(myException.info);
> ---
>
> Is it what you are looking for ?

No, normal exceptions print just fine. e.g.
writeln(new Exception("abc"));
I'm wondering what's wrong the one I defined, the error message seems to 
indicate that it doesn't have toString. It's wrong as there is one 
derived from Exception.

Workaround was to use
alias toString = Base.toString;


-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list