CException 类的一个封装

来源:互联网 发布:java计算date时间差 编辑:程序博客网 时间:2024/04/29 08:28


head.h

/********************************use sample*************************************//*       try{abc}catch(SelfCException::CException & e){rethrowException(e);}catch(std::exception & e){throwExceptionArg1(NetworkError, "Network Exception");}catch(CFileNotFound & e){e.setMsgText("can not find file:", fileName);printf("Exception is: %s\n", e.what() )}catch(...){throwExceptionArg1(CRuntimeException, "Runtime Error Exception");}/************************************************************************/#ifndef CEXCEPTION_H_#define CEXCEPTION_H_#if defined(_WIN32)#if defined(CEXCEPTION_EXPORTS)#define CEXCEPTION_API __declspec(dllexport)#else#define CEXCEPTION_API __declspec(dllimport)#endif#else#define CEXCEPTION_API#endif#include <iostream>#include <string.h>namespace SelfCException{using namespace std;#define BUFLEN 1024class CException {protected:std::string m_msgText; ///<discription of the exceptionstd::string m_whatMsg; ///<error msgstd::string m_className;///<class nameclass Location {public:std::string file; ///<file name(__FILE__)unsigned long line;///<line number(__LINE__)std::string function;///<function name};enum{ num_locations = 10 };/***@brief array of locations where the exception has been handled and thrown.*/Location m_locations[num_locations];/***@brief the current location*/int m_location_set;public:CException();CException(const char *msgFormat, ...);CException(const std::string &msg);virtual ~CException();/** *@brief get class name */const std::string getClassName();/** *@brief set msg text */void setMsgText(const char *msgFormat, ...);/** *@brief get msg text */const char* getMsgText();/** *@brief return exception msg */virtual const char *what();/** *@brief set location where the exception was raised. *@param file  name of file *@param line  line number *@param function name of function */void setLocation(const char* file, unsigned long line, const char* function = "");/** *@brief print exception to ostream */const std::ostream printfSelf(std::ostream &os);CEXCEPTION_API friend std::ostream &operator << (std::ostream &os, const CException &exception);};class CRuntimeException : public CException{CRuntimeException();CRuntimeException(const char *msgFormat, ...);CRuntimeException(const std::string &msg);virtual ~CRuntimeException();};class CFileException : public CException{CFileException();CFileException(const char *msgFormat, ...);CFileException(const std::string &msg);virtual ~CFileException();};class CNetworkException : public CException{CNetworkException();CNetworkException(const char *msgFormat, ...);CNetworkException(const std::string &msg);virtual ~CNetworkException();};#define DEFINE_EXCEPTION(A)class A : public SelfCException::CException\{\          public:\                  A() :CException(){}; \  A(const std::string &msg) : CException(msg){ m_className =#A; } \};DEFINE_EXCEPTION(CFileNotFound);DEFINE_EXCEPTION(SocketConnectFailed);DEFINE_EXCEPTION(SocketClosed);}//end SelfCException#define throwException(type)\{\type __CException__(""); \__CException__.setLocation(__FILE__, __LINE__, __FUNCTION__); \throw __CException__; \}#define throwException(type,msg)\{\type __CException__(msg); \__CException__.setLocation(__FILE__, __LINE__, __FUNCTION__); \throw __CException__; \}#define throwException(type,msgformat,msg1)\{\type __CException__(msgformat, msg1); \__CException__.setLocation(__FILE__, __LINE__, __FUNCTION__); \throw __CException__; \}#define throwException(type,msgformat,msg1,msg2)\{\type __CException__(msgformat, msg1, msg2); \__CException__.setLocation(__FILE__, __LINE__, __FUNCTION__); \throw __CException__; \}#define rethrowException(exception)\{\exception.setLocation(__FILE__, __LINE__, __FUNCTION__); \throw exception; \}#ifdef _UNIX#define throwException(type)\{\type __CException__(""); \__CException__.setLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__); \throw __CException__; \}#define throwException(type,msg)\{\type __CException__(msg); \__CException__.setLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__); \throw __CException__; \}#define throwException(type,msgformat,msg1)\{\type __CException__(msgformat, msg1); \__CException__.setLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__); \throw __CException__; \}#define throwException(type,msgformat,msg1,msg2)\{\type __CException__(msgformat, msg1, msg2); \__CException__.setLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__); \throw __CException__; \}#define rethrowException(exception)\{\exception.setLocation(__FILE__, __LINE__, __PRETTY_FUNCTION__); \throw exception; \}#endif#endif /* CEXCEPTION_H_ */

source.cpp

#include "CException.h"#include <stdio.h>#include <string.h>#include <stdarg.h>namespace SelfCException{char exceptionBuffer[BUFLEN];CException::CException():m_location_set(0){}CException::~CException() {}CException::CException(const char *msgFormat, ...):m_location_set(0){vsnprintf(exceptionBuffer, BUFLEN, msgFormat, msgFormat);m_msgText = exceptionBuffer;}CException::CException(const std::string &msg):m_location_set(0){m_msgText = exceptionBuffer;}const char *CException::what(){std::ostringstream oss;this->printfSelf(oss);m_whatMsg = oss.str();return m_whatMsg.c_str();}const std::string CException::getClassName(){return m_className;}void CException::setMsgText(const char *msgFormat, ...){vsnprintf(exceptionBuffer, BUFLEN, msgFormat, msgFormat);m_msgText = exceptionBuffer;}const char* CException::getMsgText(){return m_msgText.c_str();}void CException::setLocation(const char* file, unsigned long line, const char* function){if (m_location_set < num_locations){m_locations[m_location_set].file = std::string(file);m_locations[m_location_set].line = line;m_locations[m_location_set].function = std::string(function);m_location_set++;}}const std::ostream CException::printfSelf(std::ostream &os){os << "Exception " << std::endl;os << "Class:      " << m_className.c_str() << std::endl;os << "Exception Description:   " << m_msgText.c_str() << std::endl;if (0 != m_location_set){os << "Origin: " << std::endl;os << "    File:    " << m_locations[0].file.c_str() << std::endl;os << "    Line:    " << m_locations[0].line << std::endl;os << "    Function:" << m_locations[0].function.c_str() << std::endl;if (1 < m_location_set){os << "     path:   " << std::endl;int i = 1;while (i < m_location_set){os << "               File:    " << m_locations[0].file.c_str() << std::endl;os << "               Line:    " << m_locations[0].line << std::endl;os << "               Function:" << m_locations[0].function.c_str() << std::endl;i++;}}}return os;}CEXCEPTION_API std::ostream &operator << (std::ostream &os, const CException &exception){return exception.printfSelf(os);}//-------------------------------------------------------------------------------------------------------------CRuntimeException::CRuntimeException():CException(){m_className = "CRuntimeException";}CRuntimeException::~CRuntimeException(){}CRuntimeException::CRuntimeException(const char *msgFormat, ...):CException(){m_className = "CRuntimeException";vsnprintf(exceptionBuffer, BUFLEN, msgFormat, msgFormat);m_msgText = exceptionBuffer;}CRuntimeException::CRuntimeException(const std::string &msg):CException(){m_className = "CRuntimeException";}//-------------------------------------------------------------------------------------------------------------CFileException::CFileException():CException(){m_className = "CRuntimeException";}CFileException::~CFileException(){}CFileException::CFileException(const char *msgFormat, ...):CException(){m_className = "CRuntimeException";vsnprintf(exceptionBuffer, BUFLEN, msgFormat, msgFormat);m_msgText = exceptionBuffer;}CFileException::CFileException(const std::string &msg):CException(){m_className = "CRuntimeException";}//-------------------------------------------------------------------------------------------------------------CNetworkException::CNetworkException():CException(){m_className = "CRuntimeException";}CNetworkException::~CNetworkException(){}CNetworkException::CNetworkException(const char *msgFormat, ...):CException(){m_className = "CRuntimeException";vsnprintf(exceptionBuffer, BUFLEN, msgFormat, msgFormat);m_msgText = exceptionBuffer;}CNetworkException::CNetworkException(const std::string &msg):CException(){m_className = "CRuntimeException";}}//end namespace SelfCException


0 0
原创粉丝点击