--- /dev/null
+#ifdef WIN32
+#include <windows.h>
+#include <msp/strings/lexicalcast.h>
+#else
+#include <cerrno>
+#include <cstring>
+#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
--- /dev/null
+#ifndef MSP_CORE_SYSTEMERROR_H_
+#define MSP_CORE_SYSTEMERROR_H_
+
+#include <stdexcept>
+#include <string>
+
+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