Let's improve D's exceptions

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu May 14 11:56:33 PDT 2015


On 5/14/15 6:12 AM, Jacob Carlborg wrote:
> On 2015-05-14 00:55, Steven Schveighoffer wrote:
>
>> enforce is one of the most needless pieces of phobos:
>>
>> enforce(cond, message);
>> vs.
>> if(!cond) throw new Exception(message);
>>
>> And the second doesn't mess up inlining.
>>
>> I think enforce could be boiler-plated better. The only verbose part of
>> the if version is the throwing and newing.
>>
>> template throe(Etype = Exception)
>> {
>>     void throe(Args...)(Args args, string file = __FILE__, size_t line =
>> __LINE__)
>>     {
>>         throw new Etype(args, file, line);
>>     }
>> }
>>
>> if(!cond) throe(message);
>
> Now you're back to the same problem as "enforce" has. That it throws
> Exception by default. It shouldn't have a default value for the
> exception type.

Sure, but I wasn't arguing about that. I just don't like the utility of 
enforce completely -- it provides very little value, and kills inlining.

> BTW, it could be called "raise" as it's called in some other languages.

My symbol wasn't intended as a proposal, just something that sounds like 
"throw" but without using the keyword :) raise would be fine.

>> Wait, you're in an io package, and you want to always throw IO
>> exceptions?
>>
>> alias except = throe!IOException;
>>
>> if(!cond) except(args, to, ioexception);
>
> That is a bit better but I still think that IOException is too generic.
> Streaming something over the network and trying to open a file is quite
> different.

It was just an example, you can use whatever exception you want.

-Steve


More information about the Digitalmars-d mailing list