Sub-classing exceptions

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Thu Oct 15 11:37:17 PDT 2015


On Thursday, 15 October 2015 at 18:07:24 UTC, Shriramana Sharma 
wrote:
> So, any thoughts? Any way this could be improved? Would be nice 
> if that mixin got into the standard library somehow...

Writing a mixin to create a standard constructor for exception 
types is trivial. The problem is that you can't document code 
that's mixed in. So, the constructors wouldn't end up in the 
documentation. And while it's a bit annoying, copy-pasting the 
two standard constructors for exceptions is pretty trivial.

     /++
         Params:
             msg  = The message for the exception.
             file = The file where the exception occurred.
             line = The line number where the exception occurred.
             next = The previous exception in the chain of 
exceptions, if any.
       +/
     this(string msg,
          string file = __FILE__,
          size_t line = __LINE__,
          Throwable next = null) @safe pure nothrow
     {
         super(msg, file, line, next);
     }

     /++
         Params:
             msg  = The message for the exception.
             next = The previous exception in the chain of 
exceptions.
             file = The file where the exception occurred.
             line = The line number where the exception occurred.
       +/
     this(string msg,
          Throwable next,
          string file = __FILE__,
          size_t line = __LINE__) @safe pure nothrow
     {
         super(msg, file, line, next);
     }

So, while this is annoying, I don't think that it's a huge 
problem. We probably should have better documentation on it 
though.

However, if we were ever to get ddoc support for mixins, then we 
could easily write a function to use for mixing in the 
constructors in exception types. Heck, for exceptions that just 
have the two standard constructors, we could create a function to 
provide the code to mix in the whole type declaration. e.g.

/++
     My exception type.
   +/
mixin(genExceptionType("MyException"));

But as long as you can't put ddoc on mixins or have the ddoc 
inside a mixin end up in the documentation, we're pretty much out 
of luck on that count.

- Jonathan M Davis


More information about the Digitalmars-d mailing list