Skip to content

Instantly share code, notes, and snippets.

@Linux-cpp-lisp
Created May 24, 2012 13:23
Show Gist options
  • Save Linux-cpp-lisp/2781532 to your computer and use it in GitHub Desktop.
Save Linux-cpp-lisp/2781532 to your computer and use it in GitHub Desktop.
A set of classes used to simplify error handling with POSIX and UNIX functions in C++ programs. Header and `.cpp` files. Contains a class that works with `errno` errors, and one that works with `getaddrinfo` errors.
/*
* File: UNIXError.cpp
* Author: a
*
* Created on March 19, 2012, 5:59 PM
*/
#include "UNIXError.h"
#include <netdb.h>
UNIXError::UNIXError() : runtime_error(strerror(errno))
{
_error_num=errno;
}
UNIXError::UNIXError(int errno_c) : runtime_error(strerror(errno_c))
{
_error_num=errno;
}
int UNIXError::getErrorNum()
{
return _error_num;
}
UNIXError::UNIXError(const char* msg, int e): runtime_error(msg)
{
_error_num=e;
}
GAIError::GAIError(int gai_eint) : UNIXError(gai_strerror(gai_eint), gai_eint)
{
}
/*
* File: UNIXError.h
* Author: a
*
* Created on March 19, 2012, 5:59 PM
*/
#ifndef UNIXERROR_H
#define UNIXERROR_H
#include <errno.h>
#include <stdexcept>
#include <cstring>
using namespace std;
class UNIXError : public runtime_error {
public:
UNIXError();
explicit UNIXError(int errno_c);
int getErrorNum();
protected:
explicit UNIXError(const char* msg, int e);
int _error_num;
};
class GAIError : public UNIXError {
public:
GAIError(int gai_eint);
};
#endif /* UNIXERROR_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment