Skip to content

Commit

Permalink
Make libc_error a subclass of std::exception
Browse files Browse the repository at this point in the history
  • Loading branch information
irydacea committed Nov 23, 2015
1 parent b7f9775 commit da9a3c4
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/libc_error.hpp
Expand Up @@ -5,6 +5,7 @@
The contents of this file are placed in the public domain.
*/

#include <exception>
#include <cerrno>
#include <cstring>
#include <string>
Expand All @@ -15,10 +16,17 @@
/**
* Exception type used to propagate C runtime errors across functions.
*/
class libc_error
class libc_error : public std::exception
{
public:
libc_error(): e_(errno), msg_(strerror(e_))
libc_error()
: e_(errno)
, desc_(strerror(e_))
, msg_("C library error: " + desc_)
{
}

virtual ~libc_error() throw()
{
}

Expand All @@ -28,14 +36,21 @@ class libc_error
return e_;
}

/** Returns an explanatory string describing the runtime error. */
/** Returns an explanatory string describing the runtime error alone. */
const std::string& desc() const
{
return msg_;
return desc_;
}

/** Returns an explanatory string describing the exception. */
const char* what() const throw()
{
return msg_.c_str();
}

private:
int e_;
std::string desc_;
std::string msg_;
};

Expand Down

0 comments on commit da9a3c4

Please sign in to comment.