Let's improve D's exceptions

Adam D. Ruppe via Digitalmars-d digitalmars-d at puremagic.com
Wed May 13 18:31:21 PDT 2015


On Wednesday, 13 May 2015 at 19:24:09 UTC, Jacob Carlborg wrote:
> One thing that is _not_ making things better is "enforce" 
> which, if I recall correctly, throws Exception by default.

Aye. enforce is one of those things that looks cool but I don't 
think should actually be used.

It could perhaps be fixed...

alias enforce = enforceBase!FileException;

FILE* fp = enforce!fopen("test.txt", "rt");


The local alias tells which kind of exception is relevant in this 
context. Then the local enforce collects the arguments to the 
function and throws a new subclass of the base specific to this 
function call.

There's arguably value in that, but still, I'm not sure it is 
better than just doing it yourself.

Refresh this:
http://arsdnet.net/dcode/exception.d


It now has my enforce 2.0 proof of concept draft.

Usage:
         alias enforce = enforceBase!MyExceptionBase;
         import core.stdc.stdio;
         enforce!fopen("nofile.txt".ptr, "rb".ptr);

Message:

MyExceptionBase at exception.d(38): fopen call failed
         filename = nofile.txt
         mode = rb
----------------
stack trace here


That's actually kinda cool, automatically populating the 
exception with the arguments to the call. Perhaps of some value 
(and I don't think it will break inline since it uses a function 
alias argument instead of lazy params!)


> I think this is related: http://wiki.dlang.org/DIP33

Yeah, we could use a decent hierarchy too. Though the examples in 
there still use string concatenation to form the message, which 
is the big thing I want to get away from.


More information about the Digitalmars-d mailing list