Browse Source

new exception

Created BaseException that can act as a stringstream. You can do the following:
throw BaseException() << "some error " << variable << " foo";

Changed InvalidSettings to use BaseException, using this new syntax in Settings.
tempestpy_adaptions
gereon 12 years ago
parent
commit
a9129c00c7
  1. 40
      src/exceptions/BaseException.h
  2. 29
      src/exceptions/InvalidSettings.h
  3. 4
      src/mrmc.cpp
  4. 18
      src/utility/settings.cpp
  5. 2
      src/utility/settings.h

40
src/exceptions/BaseException.h

@ -0,0 +1,40 @@
#ifndef BASEEXCEPTION_H_
#define BASEEXCEPTION_H_
#include <exception>
#include <sstream>
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<class T>
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_

29
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 <exception>
#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_

4
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;

18
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();
}
}

2
src/utility/settings.h

@ -52,7 +52,7 @@ namespace settings {
*/
template <typename T>
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<T>();
}

Loading…
Cancel
Save