From: Rafał Długołęcki Date: Sat, 9 May 2015 14:32:26 +0000 (+0200) Subject: Cleanup exceptions. X-Git-Tag: v0.2~7 X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=commitdiff_plain;h=e516b9282a0d716b8770bdf58af13cfe0843fa92 Cleanup exceptions. --- diff --git a/include/command.h b/include/command.h index 98db429..0cbb3d6 100644 --- a/include/command.h +++ b/include/command.h @@ -6,6 +6,7 @@ #include #include "parameter.h" +#include "exception/missingRequiredParameter.h" namespace command { /** @@ -29,11 +30,11 @@ namespace command { try { matchArguments(argc, argv); } - catch(std::invalid_argument exception) { + catch(const std::invalid_argument & exception) { releaseMemory(); throw; } - catch(std::logic_error exception) { + catch(const std::logic_error & exception) { releaseMemory(); throw; } @@ -60,7 +61,7 @@ namespace command { } for(Parameter *param : parameters) { if (param->isRequired() && !param->isUsed()) { - throw std::logic_error(param->describe() + " is required but it was not passed"); + throw MissingRequiredParameter(param->describe() + " is required but it was not passed"); } } } diff --git a/include/exception/missingOptionValue.h b/include/exception/missingOptionValue.h index c305005..15d9313 100644 --- a/include/exception/missingOptionValue.h +++ b/include/exception/missingOptionValue.h @@ -7,21 +7,17 @@ namespace command { /** - * Helper template class used for releasing resources. + * Exception thrown used when Option should have value, but no one has been set */ class MissingOptionValue : public std::invalid_argument { -private: - std::string message; public: + /** \inheritdoc */ explicit MissingOptionValue(const std::string& what_arg) : - std::invalid_argument(what_arg), message(what_arg) { } + std::invalid_argument(what_arg) { } + /** \inheritdoc */ explicit MissingOptionValue(const char* what_arg) : - std::invalid_argument(what_arg), message(what_arg) { } - - virtual const char* what() const throw() { - return message.c_str(); - } + std::invalid_argument(what_arg) { } }; } diff --git a/include/exception/missingRequiredParameter.h b/include/exception/missingRequiredParameter.h new file mode 100644 index 0000000..9bc1a41 --- /dev/null +++ b/include/exception/missingRequiredParameter.h @@ -0,0 +1,25 @@ +#ifndef __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H +#define __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H + +#include +#include + +namespace command { + +/** + * Exception thrown when required argument was not set + */ +class MissingRequiredParameter : public std::logic_error { +public: + /** \inheritdoc */ + explicit MissingRequiredParameter(const std::string& what_arg) : + std::logic_error(what_arg) { } + + /** \inheritdoc */ + explicit MissingRequiredParameter(const char* what_arg) : + std::logic_error(what_arg) { } +}; + +} + +#endif /* __COMMAND_EXCEPTION_MISSING_REQUIRED_PARAMETER_H */ diff --git a/include/exception/optionFailedConversion.h b/include/exception/optionFailedConversion.h index e4004f9..335aaac 100644 --- a/include/exception/optionFailedConversion.h +++ b/include/exception/optionFailedConversion.h @@ -7,21 +7,20 @@ namespace command { /** - * Helper template class used for releasing resources. + * Exception thrown used when Option's value failed conversion to specific type + * + * e.g.: + * "a" -> int */ class OptionFailedConversion : public std::invalid_argument { -protected: - std::string message; public: + /** \inheritdoc */ explicit OptionFailedConversion(const std::string& what_arg) : - std::invalid_argument(what_arg), message(what_arg) { } + std::invalid_argument(what_arg) { } + /** \inheritdoc */ explicit OptionFailedConversion(const char* what_arg) : - std::invalid_argument(what_arg), message(what_arg) { } - - virtual const char* what() const throw() { - return message.c_str(); - } + std::invalid_argument(what_arg) { } }; }