www.pudn.com > ChatUseIOCP.rar > DtException.h


#pragma once 
 
#define DTEXCEPTION 
 
/// Base class for socket exceptions. 
class DtException 
{ 
public: 
	DtException(void) 
	{ 
		SetErrorNumber(0); 
		SetName(NULL); 
		SetDescription(NULL); 
	}; 
		 
 	~DtException(void) {}; 
 	 
	/// A constructor for our exceptionclass 
	/// @param nErrorNumber The error that were raised. Should be GetLastError or -1 for custom error. 
	/// @param Name Name of the current error 
	/// @param Description A more detailed information about the raised error. 
	DtException(int nErrorNumber, const char* pszName, const char* pszFormattedString) 
	{ 
		m_nLine = 0; 
		m_szFileName[0] = NULL; 
	 
		SetErrorNumber(nErrorNumber); 
		SetName(pszName); 
		SetDescription(pszFormattedString); 
	}; 
 
	/// Name of the exception 
	void SetName(const char* pszName) 
	{ 
		if (!pszName) 
			m_szName[0] = NULL; 
 
		if (pszName && strlen(pszName) < 256) 
			strcpy(m_szName, pszName); 
	}; 
 
	/// A descritpion of the exception. Maximum 512 bytes. 
	virtual void SetDescription(const char* pszDescription) 
	{ 
		if (!pszDescription) 
			m_szDescription[0] = NULL; 
 
		if (pszDescription && strlen(pszDescription) < 512) 
			strcpy(m_szDescription, pszDescription); 
	}; 
 
	/// Errorcode 
	virtual void SetErrorNumber(int nErrorNumber) 
	{ 
		m_nErrorNumber = nErrorNumber; 
	}; 
 
	/// Returns a nicly formatted string with all information about the error. 
	/// @returns The errormessage with all information 
	const char* ToString() 
	{ 
		if (strlen(m_szFileName) && m_nLine != 0) 
			sprintf(m_szString, "Error %d, %s %s (Filename: %s, LineNumber %d)", m_nErrorNumber, m_szName, m_szDescription, m_szFileName, m_nLine); 
		else 
			sprintf(m_szString, "Error %d, %s %s ", m_nErrorNumber, m_szName, m_szDescription); 
	 
		return m_szString; 
	}; 
 
 
	/// Filename where the exception happend (cpp file) 
	virtual void SetFileName(const char* pszFilename)  
	{ 
		if (!pszFilename) 
			m_szName[0] = NULL; 
 
		if (pszFilename && strlen(pszFilename) < 128) 
			strcpy(m_szName, pszFilename);		 
	}; 
 
	/// Line in Filename where the exception happend 
	/// @see SetFileName 
	virtual void SetLineNumber(int nLineNumber)  
	{ 
		m_nLine = nLineNumber; 
	}; 
	 
	int GetErrorNumber() const { return m_nErrorNumber; }; 
	const char* GetDescription() const { return m_szDescription; }; 
 
 
protected: 
	char m_szString[1024]; 
	char m_szName[256]; 
	char m_szDescription[512]; 
	char m_szFileName[128]; 
	int m_nErrorNumber; 
	int m_nLine; 
 
};