Custom exception

suggest change

You shouldn’t throw raw values as exceptions, instead use one of the standard exception classes or make your own.

Having your own exception class inherited from std::exception is a good way to go about it. Here’s a custom exception class which directly inherits from std::exception:

#include <exception>
#include <string>
#include <utility>

class Except : public std::exception {
    int error_number_;
    int error_offset_;
    std::string error_message_;
    
public:

    /** Constructor (C++ STL string, int, int).
     *  @param msg The error message
     *  @param err_num Error number
     *  @param err_off Error offset
     */
    explicit 
    Except(std::string message, int error_number, int error_offset)
        : error_number_(error_number),
          error_offset_(error_offset),
          error_message_(std::move(message)) {}

    /** Destructor.
     *  Virtual to allow for subclassing.
     */
    ~Except() override = default;

    /** Returns a pointer to the (constant) error description.
     *  @return A pointer to a const char. The underlying memory
     *  is in possession of the Except object. Callers must
     *  not attempt to free the memory.
     */
    const char* what() const noexcept override {
       return error_message_.c_str();
    }
    
    /** Returns error number.
     *  @return The error number.
     */
    [[nodiscard]] int getErrorNumber() const noexcept {
        return error_number_;
    }
    
    /**Returns error offset.
     * @return The error offset.
     */
    [[nodiscard]] int getErrorOffset() const noexcept {
        return error_offset_;
    }

};

An example throw catch:

try {
    throw Except{"Couldn't do what you were expecting", -12, -34};
} catch (const Except& e) {
    std::cout << e.what()
              << "\nError number: " << e.getErrorNumber()
              << "\nError offset: " << e.getErrorOffset();
}

Because the exception carries an error number and offset as well as a message, the handler can report more than the text from what().

[[nodiscard]] on the getters is a C++17 attribute. There is an exception class that lets you handle error messages: std::runtime_error

You can inherit from this class too:

#include <stdexcept>
#include <string>
#include <utility>

class Except : public std::runtime_error {
    int error_number_;
    int error_offset_;
    
public:

    /** Constructor (C++ STL string, int, int).
     *  @param msg The error message
     *  @param err_num Error number
     *  @param err_off Error offset
     */
    explicit 
    Except(std::string message, int error_number, int error_offset)
        : std::runtime_error(std::move(message)),
          error_number_(error_number),
          error_offset_(error_offset) {}
    
    /** Returns error number.
     *  @return The error number.
     */
    [[nodiscard]] int getErrorNumber() const noexcept {
        return error_number_;
    }
    
    /**Returns error offset.
     * @return The error offset.
     */
    [[nodiscard]] int getErrorOffset() const noexcept {
        return error_offset_;
    }

};

Note that this class does not override what() from std::runtime_error, so callers get the base version. You can override it if you need different text.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents