ErrnoException.enforce

Tries to evaluate expr. If it fails, checks the global errno and uses matching message as base for exception message, as well as throws the exception.

class ErrnoException
void
enforce
(
bool expr
,,
istring name = ""
,
istring file = __FILE__
,
int line = __LINE__
)

Parameters

expr bool

expression that returns false upon failure that is expected to set global errno

msg cstring

extra error message to append after main error description, can be empty

name istring

extern function name that is expected to set errno

file istring

file where the expr is evaluated

line int

line where the expr is evaluated

Throws

this if 'expr' is false

Examples

try
{
    (new ErrnoException).enforce(
        { .errno = EMFILE; return false; } (),
        "extra",
        "FUNCTION"
    );
    test(false);
}
catch (ErrnoException e)
{
    test!("==")(e.message(),
                "FUNCTION: Too many open files (extra)"[]);
    test!("==")(e.errorNumber(), EMFILE);
}

Meta