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
- CException 类的一个封装
- MFC较好地将异常封装到CException类及
- CException
- 一个封装的Ajax类
- smarty的一个封装类
- 一个封装的jacob类
- Gson封装的一个类
- 一个仿Java类封装的std::list封装类
- 使用xdebug调试YII程序的时候抛出CException异常
- 不经意间的内存泄露:try-catch(CException *e)
- 一个封装的不错的串口类
- 一个封装的实用的EXCEL类
- 一个简单的Fibonacci类的封装
- 封装的一个完整的分页类
- 封装的一个网络判断的类
- CFile家族和常用标志 CException异常类家族
- VS2010 MFC中“CException”: 不能实例化抽象类
- Part1:CException
- redhat 安装 adobe Flash
- IOS应用开发-GCD编程入门
- POJ1833 排列
- SpringMVC+Shiro权限管理
- 公式测试
- CException 类的一个封装
- C#反射概念以及实例详解
- 《C++ Primer第五版》读书笔记(6)--THE IO LIBRARY
- iOS 常用代码
- 转:AndroidManifest.xml 中的intent-filter
- 关于Eclipse调试Hadoop所出现的一些问题及解决方案
- 5个步骤帮助创业者避免功败垂成的命运
- Android系统驱动概述
- VisionMobile:2014年Q1移动开发者经济报告(一)关键信息