Report a bug
If you spot a problem with this page, click here to create a GitHub 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 a local clone.

mir.exception

@nogc exceptions and errors definitions.
Most of the API Requires DIP1008.
ref @trusted auto enforce(string fmt, string file = __FILE__, int line = __LINE__, Expr)(auto return ref scope Expr arg);
Examples:
import mir.exception;
try enforce!"Msg"(false);
catch(Exception e) assert(e.msg == "Msg");
class MirException: object.Exception;
Examples:
Generic style
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
    import mir.exception;
    try throw new MirException("Hi D", 2, "!");
    catch(MirException e) assert(e.scopeMessage == "Hi D2!");
}
Examples:
Generic style, GC allocated MSG
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
    import mir.exception;
    try throw new MirException("Hi D", 2, "!");
    catch(Exception e) assert(e.message == "Hi D2!");
}
Examples:
C++ style
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
    import mir.exception;
    import mir.format;
    try throw new MirException(stringBuf() << "Hi D" << 2 << "!" << getData);
    catch(Exception e) assert(e.scopeMessage == "Hi D2!");
}
Examples:
Low-level style
static if (__traits(compiles, (()@nogc {import mir.format;})()))
{
    import mir.exception;
    import mir.format;
    auto buffer = stringBuf();
    try throw new MirException(buf.print( "Hi D", 2, "!").data);
    catch(Exception e) assert(e.msg == "Hi D2!");
}
Examples:
@safe pure nothrow @nogc 
bool func(scope const(char)[] msg)
{
    /// scope messages are copied
    try throw new MirException(msg);
    catch(Exception e) assert(e.msg == msg);

    /// immutable strings are not copied
    static immutable char[] gmsg = "global msg";
    try throw new MirException(gmsg);
    catch(Exception e) assert(e.msg is gmsg);

    return __ctfe;
}

assert(func("runtime-time check") == 0);

static assert(func("compile-time check") == 1);
pure nothrow @nogc @trusted this(scope const(char)[] msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @trusted this(scope const(char)[] msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);

pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @safe this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);
Parameters:
const(char)[] msg message. No-scope msg is assumed to have the same lifetime as the throwable. scope strings are copied to internal buffer.
string file file name, zero terminated global string
size_t line line number
Throwable nextInChain next exception in the chain (optional)
pure this(Args...)(auto ref scope const Args args, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
if (Args.length > 1 && !is(Args[$ - 1] == Throwable));
Generic multiargument overload. Constructs a string using the print function.
class MirError: object.Error;
Examples:
@system pure nothrow @nogc 
bool func(scope const(char)[] msg)
{
    /// scope messages are copied
    try throw new MirException(msg);
    catch(Exception e) assert(e.msg == msg);

    /// immutable strings are not copied
    static immutable char[] gmsg = "global msg";
    try throw new MirError(gmsg);
    catch(Error e) assert(e.msg is gmsg);

    return __ctfe;
}

assert(func("runtime-time check") == 0);

static assert(func("compile-time check") == 1);
pure nothrow @nogc @trusted this(scope const(char)[] msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @trusted this(scope const(char)[] msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);

pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @safe this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);
Parameters:
const(char)[] msg message. No-scope msg is assumed to have the same lifetime as the throwable. scope strings are copied to internal buffer.
string file file name, zero terminated global string
size_t line line number
Throwable nextInChain next exception in the chain (optional)
pure this(Args...)(auto ref scope const Args args, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
if (Args.length > 1 && !is(Args[$ - 1] == Throwable));
Generic multiargument overload. Constructs a string using the print function.
template MirThrowableImpl()
pure nothrow @nogc @trusted this(scope const(char)[] msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @trusted this(scope const(char)[] msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);

pure nothrow @nogc @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null);

pure nothrow @nogc @safe this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__);
Parameters:
const(char)[] msg message. No-scope msg is assumed to have the same lifetime as the throwable. scope strings are copied to internal buffer.
string file file name, zero terminated global string
size_t line line number
Throwable nextInChain next exception in the chain (optional)
pure this(Args...)(auto ref scope const Args args, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null)
if (Args.length > 1 && !is(Args[$ - 1] == Throwable));
Generic multiargument overload. Constructs a string using the print function.
enum int maxMirExceptionMsgLen;