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
.
#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;
}
I would appreciate very much any kind of comments / help.