Errors#

class unit::error : public std::exception#

Exception thrown by all UNIT C++ operations on failure. Wraps the error code and message from the C library. See UNIT_ErrorCode and UNIT_GetErrorMessage().

const char *what() const noexcept#

Return the error message from the C library.

UNIT_ErrorCode code() const noexcept#

Return the error code.

Example#
try {
    unit::Context ctx;
    unit::Procedure proc(ctx, "main");
    proc.load_integer(42);
    proc.return_value();
    auto compiled = proc.compile(unit::Platform::host());
} catch (const unit::error &e) {
    std::cerr << "UNIT error: " << e.what() << std::endl;
    // e.code() == UNIT_ERROR_INVALID_USAGE, etc.
}

All methods on Context, Procedure, CompiledProcedure, and SymbolMap throw unit::error on failure. The only exception is Context::Context(), which throws std::runtime_error because there is no context to extract an error from.

enum class unit::ErrorCode : int#

Error codes returned by error::code(). See UNIT_ErrorCode.

enumerator None = UNIT_ERROR_NONE#

No error is set.

enumerator NoMemory = UNIT_ERROR_NO_MEMORY#

A memory allocation failed.

enumerator InvalidUsage = UNIT_ERROR_INVALID_USAGE#

An API was misused by the caller. Check error::what() for details.

enumerator OSFailure = UNIT_ERROR_OS_FAILURE#

A call to an operating system API failed.

enumerator UnsupportedPlatform = UNIT_ERROR_UNSUPPORTED_PLATFORM#

An operation is not available on this platform.

Example#
try {
    auto compiled = proc.compile(unit::Platform::host());
} catch (const unit::error &e) {
    if (e.code() == unit::ErrorCode::UnsupportedPlatform) {
        std::cerr << "platform not supported" << std::endl;
    }
}