From: Mikko Rasa Date: Sat, 28 May 2011 10:12:34 +0000 (+0300) Subject: New system_error exception class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6a38983c19fe78753962288e206c5817ad595448;p=libs%2Fcore.git New system_error exception class --- diff --git a/source/core/systemerror.cpp b/source/core/systemerror.cpp new file mode 100644 index 0000000..6d1d876 --- /dev/null +++ b/source/core/systemerror.cpp @@ -0,0 +1,38 @@ +#ifdef WIN32 +#include +#include +#else +#include +#include +#endif +#include "systemerror.h" + +using namespace std; + +namespace Msp { + +system_error::system_error(const string &w, int c): + runtime_error(w+": "+get_message(c)), + code_(c) +{ } + +string system_error::get_message(int c) +{ +#ifdef WIN32 + if(c==-1) + c = GetLastError(); + + char msg[1024]; + if(FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, c, 0, msg, sizeof(msg), 0)) + return msg; + else + return lexical_cast(c, Fmt().hex()); +#else + if(c==-1) + c = errno; + + return strerror(c); +#endif +} + +} // namespace Msp diff --git a/source/core/systemerror.h b/source/core/systemerror.h new file mode 100644 index 0000000..4b7493d --- /dev/null +++ b/source/core/systemerror.h @@ -0,0 +1,26 @@ +#ifndef MSP_CORE_SYSTEMERROR_H_ +#define MSP_CORE_SYSTEMERROR_H_ + +#include +#include + +namespace Msp { + +class system_error: public std::runtime_error +{ +private: + int code_; + +public: + system_error(const std::string &, int = -1); + virtual ~system_error() throw() { } + + int code() const throw() { return code_; } + +private: + static std::string get_message(int); +}; + +} // namespace Msp + +#endif