From bc6f456ef27bdf25bf7a7f677217b9b7204b4241 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 13 May 2006 02:24:07 +0000 Subject: Added a nice, generic exception class. It really helps out a lot. I dunno if it's better to create a new subclass of it for each situation, but it is cool... --- src/exception.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/exception.h | 22 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 src/exception.cpp create mode 100644 src/exception.h (limited to 'src') diff --git a/src/exception.cpp b/src/exception.cpp new file mode 100644 index 0000000..11f04a9 --- /dev/null +++ b/src/exception.cpp @@ -0,0 +1,44 @@ +#include "exception.h" +#include + +Exception::Exception( const char *lpFormat, ... ) throw() : + nErrorCode( 0 ) +{ + va_list ap; + int nSize; + + va_start(ap, lpFormat); + nSize = vsnprintf( NULL, 0, lpFormat, ap ); + sWhat = new char[nSize+1]; + vsnprintf( sWhat, nSize+1, lpFormat, ap ); + va_end(ap); +} + +Exception::Exception( int nCode, const char *lpFormat, ... ) throw() : + nErrorCode( nCode ) +{ + va_list ap; + int nSize; + + va_start(ap, lpFormat); + nSize = vsnprintf( NULL, 0, lpFormat, ap ); + sWhat = new char[nSize+1]; + vsnprintf( sWhat, nSize+1, lpFormat, ap ); + va_end(ap); +} + +Exception::~Exception() throw() +{ + delete[] sWhat; +} + +const char *Exception::what() const throw() +{ + return sWhat; +} + +int Exception::getErrorCode() +{ + return nErrorCode; +} + diff --git a/src/exception.h b/src/exception.h new file mode 100644 index 0000000..be876d7 --- /dev/null +++ b/src/exception.h @@ -0,0 +1,22 @@ +#ifndef EXCEPTION_H +#define EXCEPTION_H + +#include +#include + +class Exception : public std::exception +{ +public: + Exception( const char *sFormat, ... ) throw(); + Exception( int nCode, const char *sFormat, ... ) throw(); + virtual ~Exception() throw(); + + virtual const char *what() const throw(); + int getErrorCode(); + +private: + char *sWhat; + int nErrorCode; +}; + +#endif -- cgit v1.2.3