EXT C++ Library - throw error

The error macro allows you to elegantly throw (or to create an object of) exception containing file name and line where the problem occured.


Details:

The error macro simply expands into construction of the ext::local_error object which is indirectly derived from std::exception. Thus you may also use new error to create an exception object. The ext::local_error type is derived from ext::runtime_error. See "extexcept" for further details.

The error construction is implemented as a macro defined in "ext/ext_bits/error" file, to use the feature include the <ext/c++> header file.

Interface:

class ext::local_error : public ext::runtime_error {
    public:
        const std::string   file;
        const std::string   func;
        const unsigned int  line;

    // inherited members
    // public:
    //     const unsigned int  code;
    //     const char * what () const throw ();

    public:
        local_error (const std::string &, const std::string &, unsigned int);
};

Examples:

#include <ext/c++>

int main () {
    try {
        // ...
        if (!/*...*/)
            throw error;
        // ...
        if (!/*...*/)
            throw error;
        // ...
        if (!/*...*/)
            throw error;
        // ...
    } catch (const ext::local_error & e) {
        std::printf ("%s :: %s [%u]",
                     e.file.c_str (), e.func.c_str () e.line);
    };
};

Remarks

Because the error is implemented as a macro, you will experience difficulties if you are using the word as a variable name or a type name. To fix these, you will need to either update your codes not to use error in constructs other than throw error (or potentaily new error), or your can define EXT_FAIR_MACROS and use ERROR instead then.

If you decided not to use error at all, you can disable it by defining EXT_NO_ERROR before including <ext/c++>. You can also disable all macros by defining EXT_NO_MACROS.


[index]