现在的位置: 首页 > 综合 > 正文

自己的异常类

2018年02月08日 ⁄ 综合 ⁄ 共 2880字 ⁄ 字号 评论关闭
#pragma once
#include <string>

#ifndef __FUNCTION__
#define __FUNCTION__ "Global"
#endif

#pragma warning (disable : 4290)								///< 屏蔽 4290 警告 因为微软的c++ 编译器没有支持 这个规范

#define  ThrowException(Descript,code) throw Exception_CH(Descript,code,__FILE__, __FUNCTION__ ,__LINE__)
#define  ThrowException0(Descript) throw Exception_CH(Descript,Exception_CH::NON,__FILE__, __FUNCTION__ ,__LINE__)
#define  ThrowException1(code) throw Exception_CH("",code,__FILE__, __FUNCTION__ ,__LINE__)
#define  ThrowException2() throw Exception_CH("",Exception_CH::NON,__FILE__, __FUNCTION__ ,__LINE__)

class Exception_CH
{
public:
	enum ExceptionCode_e
	{
		NON,
		OpenPort,							///< 打开端口失败
		APIReadFile, 						///< 调用API ReadFile 出错
		APIWriteFile,						///< 调用API WriteFile 出错
		ErrorData,							///< 返回的数据错误
		SMSDecode,						///<短信解码错误
		Matched,							///<正则表达式匹配错误
	};
public:
	Exception_CH(const std::string & strDescript,ExceptionCode_e Code,char* szFile,char* szFun,int nLine);
	Exception_CH(const Exception_CH & other);
	Exception_CH& operator=(const Exception_CH & other);
	virtual ~Exception_CH(void);
	const std::string & what() const; ///< 获取异常描述字串
	std::string DebugStr() const; ///<获得一个可 在output 窗口 双击定位的 字串
	int GetLastErrorCode(); ///<获取到   系统API GetLastError 获取到的代码
	ExceptionCode_e GetExeptionCode();						///< 获取异常代码
	void PrintDebugInfo();						///< 打印调试信息


private:
	std::string m_strFile;						///< 发生异常的文件
	std::string m_strFun;						///< 发生异常的函数
	int m_nLine;									///< 发生异常所在文件的行数
	std::string m_strDescript; 				///< 异常描述字符串
	ExceptionCode_e m_Code;			 ///<异常代码 
	int m_nLastError;							///< 系统API GetLastError 获取到的代码

};




#include "Exception_CH.h"
#include <sstream>
#ifdef WIN32
#include <Windows.h>
#include <tchar.h>
#include <comutil.h>
#pragma comment(lib,"comsuppw.lib")
#endif

Exception_CH::Exception_CH(const std::string & strDescript,ExceptionCode_e Code,char* szFile,char* szFun,int nLine)
:m_strFile(szFile)
,m_strFun(szFun)
,m_nLine(nLine)
,m_strDescript(strDescript)
,m_Code(Code)
{
#ifdef WIN32
	m_nLastError = ::GetLastError();
#else
	m_nLastError = 0;
#endif

#ifdef _DEBUG
	this->PrintDebugInfo();
#endif
}

Exception_CH::Exception_CH(const Exception_CH & other)
{
	*this = other;
}
 Exception_CH& Exception_CH::operator=(const Exception_CH & other)
{
	if (this == &other)
	{
		return *this;
	}
	this->m_strFile = other.m_strFile;
	this->m_strFun = other.m_strFun;
	this->m_nLine =  other.m_nLine;
	this->m_strDescript =  other.m_strDescript;
	this->m_Code = other.m_Code;
	this->m_nLastError = other.m_nLastError;
	return *this;
}
Exception_CH::~Exception_CH(void)
{
}

const std::string & Exception_CH::what() const
{	
	return m_strDescript;
}
std::string Exception_CH::DebugStr() const
{
	std::stringstream strDes;
	strDes<<m_strFile<<"("<<m_nLine<<")"<<std::endl\
		<<"Depict:  "<<m_strDescript<<"    ExcpeptionCode  :"<<m_Code<<"    LastErrorCode:  "<<m_nLastError<<std::endl;
	return strDes.str();
}

int Exception_CH::GetLastErrorCode()
{
	return m_nLastError;
}

Exception_CH::ExceptionCode_e Exception_CH::GetExeptionCode()
{
	return m_Code;
}

void  Exception_CH::PrintDebugInfo()
{
#ifdef _WIN32
	OutputDebugString(_T("\n"));
#endif
#ifdef _UNICODE
	_bstr_t t =  this->DebugStr().c_str();
	wchar_t *Degstr = (wchar_t *)t;	
	OutputDebugStringW(Degstr);
#else
	OutputDebugStringA(this->DebugStr().c_str());
#endif
}

抱歉!评论已关闭.