View source code
Display the source code in std/exception.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Template std.exception.basicExceptionCtors

Convenience mixin for trivially sub-classing exceptions

template basicExceptionCtors ;

Even trivially sub-classing an exception involves writing boilerplate code for the constructor to: 1) correctly pass in the source file and line number the exception was thrown from; 2) be usable with enforce which expects exception constructors to take arguments in a fixed order. This mixin provides that boilerplate code.

Note however that you need to mark the mixin line with at least a minimal (i.e. just ///) DDoc comment if you want the mixed-in constructors to be documented in the newly created Exception subclass.

Current limitation: Due to bug #11500, currently the constructors specified in this mixin cannot be overloaded with any other custom constructors. Thus this mixin can currently only be used when no such custom constructors need to be explicitly specified.

Contained Functions



class MeaCulpa: Exception
    mixin basicExceptionCtors;

    throw new MeaCulpa("test");
catch (MeaCulpa e)
    writeln(e.msg); // "test"
    writeln(e.file); // __FILE__
    writeln(e.line); // __LINE__ - 5


Andrei Alexandrescu and Jonathan M Davis


Boost License 1.0