diff --git a/src/exceptions/BaseException.h b/src/exceptions/BaseException.h new file mode 100644 index 000000000..1f61c8338 --- /dev/null +++ b/src/exceptions/BaseException.h @@ -0,0 +1,40 @@ +#ifndef BASEEXCEPTION_H_ +#define BASEEXCEPTION_H_ + +#include +#include + +namespace mrmc { +namespace exceptions { + +class BaseException : public std::exception +{ + public: + BaseException() : exception() {} + BaseException(const BaseException& cp) + : exception(cp), stream(cp.stream.str()) + { + } + + ~BaseException() throw() { } + + template + BaseException& operator<<(const T& var) + { + this->stream << var; + return *this; + } + + virtual const char* what() const throw() + { + return this->stream.str().c_str(); + } + + private: + std::stringstream stream; +}; + +} // namespace exceptions +} // namespace mrmc + +#endif // BASEEXCEPTION_H_ diff --git a/src/exceptions/InvalidSettings.h b/src/exceptions/InvalidSettings.h index 8244c6d4c..2e34a7419 100644 --- a/src/exceptions/InvalidSettings.h +++ b/src/exceptions/InvalidSettings.h @@ -1,35 +1,16 @@ -#ifndef MRMC_EXCEPTIONS_INVALID_SETTINGS_H_ -#define MRMC_EXCEPTIONS_INVALID_SETTINGS_H_ +#ifndef INVALIDSETTINGS_H_ +#define INVALIDSETTINGS_H_ -#include +#include "src/exceptions/BaseException.h" namespace mrmc { namespace exceptions { -//!This exception is thrown when a memory request can't be -//!fulfilled. -class InvalidSettings : public std::exception +class InvalidSettings : public BaseException { - public: -/* The Visual C++-Version of the exception class has constructors accepting - * a char*-constant; The GCC version does not have these - * - * As the "extended" constructor is used in the sparse matrix code, a dummy - * constructor is used under linux (which will ignore the parameter) - */ -#ifdef _WIN32 - InvalidSettings() : exception("::mrmc::InvalidSettings"){} - InvalidSettings(const char * const s): exception(s) {} -#else - InvalidSettings() : exception() {} - InvalidSettings(const char * const s): exception() {} - -#endif - virtual const char* what() const throw() - { return "mrmc::InvalidSettings"; } }; } // namespace exceptions } // namespace mrmc -#endif // MRMC_EXCEPTIONS_INVALID_SETTINGS_H_ +#endif // INVALIDSETTINGS_H_ diff --git a/src/mrmc.cpp b/src/mrmc.cpp index 243f1e0cc..6f2caed40 100644 --- a/src/mrmc.cpp +++ b/src/mrmc.cpp @@ -75,8 +75,10 @@ int main(const int argc, const char* argv[]) { try { s = mrmc::settings::newInstance(argc, argv, nullptr); - } catch (mrmc::exceptions::InvalidSettings&) { + } catch (mrmc::exceptions::InvalidSettings& e) { + LOG4CPLUS_FATAL(logger, "InvalidSettings error: " << e.what()); LOG4CPLUS_FATAL(logger, "Could not recover from settings error, terminating."); + std::cout << "Could not recover from settings error: " << e.what() << std::endl; std::cout << std::endl << mrmc::settings::help; delete s; return 1; diff --git a/src/utility/settings.cpp b/src/utility/settings.cpp index 748bc931a..94aba1f15 100644 --- a/src/utility/settings.cpp +++ b/src/utility/settings.cpp @@ -7,6 +7,8 @@ #include "src/utility/settings.h" +#include "src/exceptions/BaseException.h" + #include "log4cplus/logger.h" #include "log4cplus/loggingmacros.h" extern log4cplus::Logger logger; @@ -107,27 +109,19 @@ Settings::Settings(const int argc, const char* argv[], const char* filename) } catch (bpo::required_option e) { - std::cerr << "Required option: " << e.what() << std::endl; - LOG4CPLUS_ERROR(logger, "Required option: " << e.what()); - throw mrmc::exceptions::InvalidSettings(); + throw mrmc::exceptions::InvalidSettings() << "Required option missing"; } catch (bpo::validation_error e) { - std::cerr << "Validation failed: " << e.what() << std::endl; - LOG4CPLUS_ERROR(logger, "Validation failed: " << e.what()); - throw mrmc::exceptions::InvalidSettings(); + throw mrmc::exceptions::InvalidSettings() << "Validation failed: " << e.what(); } catch (bpo::invalid_command_line_syntax e) { - std::cerr << "Invalid command line syntax: " << e.what() << std::endl; - LOG4CPLUS_ERROR(logger, "Invalid command line syntax: " << e.what()); - throw mrmc::exceptions::InvalidSettings(); + throw mrmc::exceptions::InvalidSettings() << e.what(); } catch (bpo::error e) { - std::cerr << e.what() << std::endl; - LOG4CPLUS_ERROR(logger, "Unknown error: " << e.what()); - throw mrmc::exceptions::InvalidSettings(); + throw mrmc::exceptions::InvalidSettings() << e.what(); } } diff --git a/src/utility/settings.h b/src/utility/settings.h index 842074655..0d634697f 100644 --- a/src/utility/settings.h +++ b/src/utility/settings.h @@ -52,7 +52,7 @@ namespace settings { */ template const T& get(const std::string &name) const { - if (this->vm.count(name) == 0) throw mrmc::exceptions::InvalidSettings(); + if (this->vm.count(name) == 0) throw mrmc::exceptions::InvalidSettings() << "Could not read option " << name << "."; return this->vm[name].as(); }