Skip to main content
Tweeted twitter.com/StackCodeReview/status/1357071535849287683
Edit spelling, grammer; remove fluff
Source Link
1201ProgramAlarm
  • 7.8k
  • 2
  • 23
  • 39

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest to me how I could improve it? Or maybe I have went the wrong direction? (Also it would be amazing to know how I could make this peacepiece of code more of a modern C++.)

Here is a simple namespace with classes, which inherentinherits from std::error_category and std::system_error.

I would appreciate very much any kind of comments / help. Thank you very much in advance!

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest me how I could improve it? Or maybe I have went the wrong direction? (Also would be amazing to know how I could make this peace of code more of a modern C++)

Here is simple namespace with classes, which inherent from std::error_category and std::system_error.

I would appreciate very much any kind of comments / help. Thank you very much in advance!

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest to me how I could improve it? Or maybe I have went the wrong direction? (Also it would be amazing to know how I could make this piece of code more of a modern C++.)

Here is a simple namespace with classes, which inherits from std::error_category and std::system_error.

I would appreciate very much any kind of comments / help.

added 10 characters in body; edited tags
Source Link
L. F.
  • 9.7k
  • 2
  • 27
  • 70

Cpp C++ custom exception handling using std::error_category and std::system_error

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest me how I could improve it  ? Or maybe I have went the wrong direction? (Also would be amazing to know how I could make this peace of code more of a modern c++C++)

Here is simple namespace with classes, which inherent from std::error_categorystd::error_category and std::system_errorstd::system_error.

#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}
#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}

Cpp exception handling

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest me how I could improve it  ? Or maybe I have went the wrong direction? (Also would be amazing to know how I could make this peace of code more of a modern c++)

Here is simple namespace with classes, which inherent from std::error_category and std::system_error.

#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}

C++ custom exception handling using std::error_category and std::system_error

I want to make my own custom exception handling and I am curious if I am going the right path, maybe some of you could suggest me how I could improve it? Or maybe I have went the wrong direction? (Also would be amazing to know how I could make this peace of code more of a modern C++)

Here is simple namespace with classes, which inherent from std::error_category and std::system_error.

#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}
added 204 characters in body
Source Link
Widok
  • 43
  • 4

Here is simple namespace with classes, which inherent from std::error_category and std::system_error.

#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}
 

Main.cpp, which invokes methods throwing exceptions from previously declared namespace.

using namespace CustomException;

void initEngine() {
    throw EngineError(EngineErrorCode::FAILED_INIT_ENGINE, "Failed init engine");
}

void initBreaks() {
    throw BreakError(BreakErrorCode::FAILED_INIT_BREAKS, "Failed init breaks");
}

int main() {
    try {
        initEngine();
        initBreaks();
    }
    catch (const BreakError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (const EngineError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (...) {
        std::cout << "Caught something unexpected error" << std::endl;
    }
    return 0;
}
#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}
 
using namespace CustomException;

void initEngine() {
    throw EngineError(EngineErrorCode::FAILED_INIT_ENGINE, "Failed init engine");
}

void initBreaks() {
    throw BreakError(BreakErrorCode::FAILED_INIT_BREAKS, "Failed init breaks");
}

int main() {
    try {
        initEngine();
        initBreaks();
    }
    catch (const BreakError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (const EngineError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (...) {
        std::cout << "Caught something unexpected error" << std::endl;
    }
    return 0;
}

Here is simple namespace with classes, which inherent from std::error_category and std::system_error.

#include <string>
#include <system_error>
#include <iostream>

namespace CustomException {

    class ErrorC : public std::error_category {
        using Base = std::error_category;
    public:
        char const *name() const noexcept
        override { return "App error"; }

        std::error_condition default_error_condition(int const code) const noexcept
        override {
            (void) code;
            return {};
        }

        bool equivalent(int const code, std::error_condition const &condition) const noexcept
        override {
            (void) code;
            (void) condition;
            return false;
        }

        bool equivalent(std::error_code const &code, int const condition) const noexcept
        override { return Base::equivalent(code, condition); }

        std::string message(int const condition) const
        override { return "An application error occurred, code = " + std::to_string(condition); }

        constexpr ErrorC() : Base{} {}
    };

    auto app_error_category()
    -> ErrorC const & {
        static ErrorC the_instance;
        return the_instance;
    }


    enum class BreakErrorCode {
        FAILED_INIT_BREAKS = 1,
        FAILED_TO_LINK_BREAKS = 2,
    };

    class BreakError : public std::system_error {
        using Base = std::system_error;
    public:
        BreakErrorCode appErrorCode() const {
            return static_cast<BreakErrorCode>(code().value());
        }

        explicit BreakError(const BreakErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        BreakError(const BreakErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };


    enum class EngineErrorCode {
        FAILED_INIT_ENGINE = 101,
        MOTHER_BOARD_FAILED_LINKING = 102,
    };

    class EngineError : public std::system_error {
        using Base = std::system_error;
    public:
        EngineErrorCode appErrorCode() const {
            return static_cast<EngineErrorCode>(code().value());
        }

        EngineError(const EngineErrorCode code)
                : Base{(int) code, app_error_category()} {
        }

        EngineError(const EngineErrorCode code, const std::string &description)
                : Base{(int) code, app_error_category(), description} {
        }
    };

    enum class ControlErrorCode {
        CONTROL_GIMBAL_FAILED = 301,
        OBTAIN_CONTROL_AUTHORITY_FAILED = 302,
    };

}

Main.cpp, which invokes methods throwing exceptions from previously declared namespace.

using namespace CustomException;

void initEngine() {
    throw EngineError(EngineErrorCode::FAILED_INIT_ENGINE, "Failed init engine");
}

void initBreaks() {
    throw BreakError(BreakErrorCode::FAILED_INIT_BREAKS, "Failed init breaks");
}

int main() {
    try {
        initEngine();
        initBreaks();
    }
    catch (const BreakError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (const EngineError &error) {
        std::cout << error.what() << std::endl;
    }
    catch (...) {
        std::cout << "Caught something unexpected error" << std::endl;
    }
    return 0;
}
Source Link
Widok
  • 43
  • 4
Loading