Feature Request #1873924

Add code (int) to Poco::Exception with default value zero. When throwing exceptions, applications can currently set a custom message. This will add the ability to set a custom code as well, that is specific to the application. This change implements codes for Poco::SocketImpl via the error() method to allow handling of different socket errors based on their actual type (without having to compare their message text).
This commit is contained in:
Eran Hammer-Lahav 2008-01-17 19:07:35 +00:00
parent 19dca2aa79
commit 1f47c0df2f
3 changed files with 109 additions and 97 deletions

View File

@ -52,13 +52,13 @@ class Foundation_API Exception: public std::exception
/// in the Poco class library.
{
public:
Exception(const std::string& msg);
Exception(const std::string& msg, int code = 0);
/// Creates an exception.
Exception(const std::string& msg, const std::string& arg);
Exception(const std::string& msg, const std::string& arg, int code = 0);
/// Creates an exception.
Exception(const std::string& msg, const Exception& nested);
Exception(const std::string& msg, const Exception& nested, int code = 0);
/// Creates an exception and stores a clone
/// of the nested exception.
@ -88,6 +88,9 @@ public:
const std::string& message() const;
/// Returns the message text.
int code() const;
/// Returns the exception code if defined.
std::string displayText() const;
/// Returns a string consisting of the
@ -107,12 +110,13 @@ public:
/// throwing it again.
protected:
Exception();
Exception(int code = 0);
/// Standard constructor.
private:
std::string _msg;
Exception* _pNested;
int _code;
};
@ -131,6 +135,12 @@ inline const std::string& Exception::message() const
}
inline int Exception::code() const
{
return _code;
}
//
// Macros for quickly declaring and implementing exception classes.
// Unfortunately, we cannot use a template here because character
@ -138,62 +148,62 @@ inline const std::string& Exception::message() const
// are not allowed as template arguments.
//
#define POCO_DECLARE_EXCEPTION(API, CLS, BASE) \
class API CLS: public BASE \
{ \
public: \
CLS(); \
CLS(const std::string& msg); \
CLS(const std::string& msg, const std::string& arg); \
CLS(const std::string& msg, const Poco::Exception& exc); \
CLS(const CLS& exc); \
~CLS() throw(); \
CLS& operator = (const CLS& exc); \
const char* name() const throw(); \
const char* className() const throw(); \
Poco::Exception* clone() const; \
void rethrow() const; \
class API CLS: public BASE \
{ \
public: \
CLS(int code = 0); \
CLS(const std::string& msg, int code = 0); \
CLS(const std::string& msg, const std::string& arg, int code = 0); \
CLS(const std::string& msg, const Poco::Exception& exc, int code = 0); \
CLS(const CLS& exc); \
~CLS() throw(); \
CLS& operator = (const CLS& exc); \
const char* name() const throw(); \
const char* className() const throw(); \
Poco::Exception* clone() const; \
void rethrow() const; \
};
#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
CLS::CLS() \
{ \
} \
CLS::CLS(const std::string& msg): BASE(msg) \
{ \
} \
CLS::CLS(const std::string& msg, const std::string& arg): BASE(msg, arg) \
{ \
} \
CLS::CLS(const std::string& msg, const Poco::Exception& exc): BASE(msg, exc) \
{ \
} \
CLS::CLS(const CLS& exc): BASE(exc) \
{ \
} \
CLS::~CLS() throw() \
{ \
} \
CLS& CLS::operator = (const CLS& exc) \
{ \
BASE::operator = (exc); \
return *this; \
} \
const char* CLS::name() const throw() \
{ \
return NAME; \
} \
const char* CLS::className() const throw() \
{ \
return typeid(*this).name(); \
} \
Poco::Exception* CLS::clone() const \
{ \
return new CLS(*this); \
} \
void CLS::rethrow() const \
{ \
throw *this; \
#define POCO_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
CLS::CLS(int code): BASE(code) \
{ \
} \
CLS::CLS(const std::string& msg, int code): BASE(msg, code) \
{ \
} \
CLS::CLS(const std::string& msg, const std::string& arg, int code): BASE(msg, arg, code) \
{ \
} \
CLS::CLS(const std::string& msg, const Poco::Exception& exc, int code): BASE(msg, exc, code) \
{ \
} \
CLS::CLS(const CLS& exc): BASE(exc) \
{ \
} \
CLS::~CLS() throw() \
{ \
} \
CLS& CLS::operator = (const CLS& exc) \
{ \
BASE::operator = (exc); \
return *this; \
} \
const char* CLS::name() const throw() \
{ \
return NAME; \
} \
const char* CLS::className() const throw() \
{ \
return typeid(*this).name(); \
} \
Poco::Exception* CLS::clone() const \
{ \
return new CLS(*this); \
} \
void CLS::rethrow() const \
{ \
throw *this; \
}

View File

@ -41,17 +41,17 @@
namespace Poco {
Exception::Exception(): _pNested(0)
Exception::Exception(int code): _pNested(0), _code(code)
{
}
Exception::Exception(const std::string& msg): _msg(msg), _pNested(0)
Exception::Exception(const std::string& msg, int code): _msg(msg), _pNested(0), _code(code)
{
}
Exception::Exception(const std::string& msg, const std::string& arg): _msg(msg), _pNested(0)
Exception::Exception(const std::string& msg, const std::string& arg, int code): _msg(msg), _pNested(0), _code(code)
{
if (!arg.empty())
{
@ -61,7 +61,7 @@ Exception::Exception(const std::string& msg, const std::string& arg): _msg(msg),
}
Exception::Exception(const std::string& msg, const Exception& nested): _msg(msg), _pNested(nested.clone())
Exception::Exception(const std::string& msg, const Exception& nested, int code): _msg(msg), _pNested(nested.clone()), _code(code)
{
}
@ -70,6 +70,7 @@ Exception::Exception(const Exception& exc): std::exception(exc)
{
_msg = exc._msg;
_pNested = exc._pNested ? exc._pNested->clone() : 0;
_code = exc._code;
}
@ -86,6 +87,7 @@ Exception& Exception::operator = (const Exception& exc)
delete _pNested;
_msg = exc._msg;
_pNested = exc._pNested ? exc._pNested->clone() : 0;
_code = exc._code;
}
return *this;
}

View File

@ -799,77 +799,77 @@ void SocketImpl::error(int code, const std::string& arg)
switch (code)
{
case POCO_ESYSNOTREADY:
throw NetException("Net subsystem not ready");
throw NetException("Net subsystem not ready", code);
case POCO_ENOTINIT:
throw NetException("Net subsystem not initialized");
throw NetException("Net subsystem not initialized", code);
case POCO_EINTR:
throw IOException("Interrupted");
throw IOException("Interrupted", code);
case POCO_EACCES:
throw IOException("Permission denied");
throw IOException("Permission denied", code);
case POCO_EFAULT:
throw IOException("Bad address");
throw IOException("Bad address", code);
case POCO_EINVAL:
throw InvalidArgumentException();
throw InvalidArgumentException(code);
case POCO_EMFILE:
throw IOException("Too many open files");
throw IOException("Too many open files", code);
case POCO_EWOULDBLOCK:
throw IOException("Operation would block");
throw IOException("Operation would block", code);
case POCO_EINPROGRESS:
throw IOException("Operation now in progress");
throw IOException("Operation now in progress", code);
case POCO_EALREADY:
throw IOException("Operation already in progress");
throw IOException("Operation already in progress", code);
case POCO_ENOTSOCK:
throw IOException("Socket operation attempted on non-socket");
throw IOException("Socket operation attempted on non-socket", code);
case POCO_EDESTADDRREQ:
throw NetException("Destination address required");
throw NetException("Destination address required", code);
case POCO_EMSGSIZE:
throw NetException("Message too long");
throw NetException("Message too long", code);
case POCO_EPROTOTYPE:
throw NetException("Wrong protocol type");
throw NetException("Wrong protocol type", code);
case POCO_ENOPROTOOPT:
throw NetException("Protocol not available");
throw NetException("Protocol not available", code);
case POCO_EPROTONOSUPPORT:
throw NetException("Protocol not supported");
throw NetException("Protocol not supported", code);
case POCO_ESOCKTNOSUPPORT:
throw NetException("Socket type not supported");
throw NetException("Socket type not supported", code);
case POCO_ENOTSUP:
throw NetException("Operation not supported");
throw NetException("Operation not supported", code);
case POCO_EPFNOSUPPORT:
throw NetException("Protocol family not supported");
throw NetException("Protocol family not supported", code);
case POCO_EAFNOSUPPORT:
throw NetException("Address family not supported");
throw NetException("Address family not supported", code);
case POCO_EADDRINUSE:
throw NetException("Address already in use", arg);
throw NetException("Address already in use", arg, code);
case POCO_EADDRNOTAVAIL:
throw NetException("Cannot assign requested address", arg);
throw NetException("Cannot assign requested address", arg, code);
case POCO_ENETDOWN:
throw NetException("Network is down");
throw NetException("Network is down", code);
case POCO_ENETUNREACH:
throw NetException("Network is unreachable");
throw NetException("Network is unreachable", code);
case POCO_ENETRESET:
throw NetException("Network dropped connection on reset");
throw NetException("Network dropped connection on reset", code);
case POCO_ECONNABORTED:
throw ConnectionAbortedException();
throw ConnectionAbortedException(code);
case POCO_ECONNRESET:
throw ConnectionResetException();
throw ConnectionResetException(code);
case POCO_ENOBUFS:
throw IOException("No buffer space available");
throw IOException("No buffer space available", code);
case POCO_EISCONN:
throw NetException("Socket is already connected");
throw NetException("Socket is already connected", code);
case POCO_ENOTCONN:
throw NetException("Socket is not connected");
throw NetException("Socket is not connected", code);
case POCO_ESHUTDOWN:
throw NetException("Cannot send after socket shutdown");
throw NetException("Cannot send after socket shutdown", code);
case POCO_ETIMEDOUT:
throw TimeoutException();
throw TimeoutException(code);
case POCO_ECONNREFUSED:
throw ConnectionRefusedException(arg);
throw ConnectionRefusedException(arg, code);
case POCO_EHOSTDOWN:
throw NetException("Host is down");
throw NetException("Host is down", code);
case POCO_EHOSTUNREACH:
throw NetException("No route to host");
throw NetException("No route to host", code);
default:
throw IOException(NumberFormatter::format(code) + arg);
throw IOException(NumberFormatter::format(code) + arg, code);
}
}