Chinaunix首页 | 论坛 | 博客
  • 博客访问: 381423
  • 博文数量: 715
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:46
文章分类

全部博文(715)

文章存档

2011年(1)

2008年(714)

我的朋友

分类:

2008-10-13 16:31:12

写程序免不了有异常,没碰到过异常的请举手(哦,新来的啊,好,坐下),好闲话少说,看说明。
这个异常类,在程序出错的时候会输出一些信息,比如发生错误的函数(自填),错误描述(自填如果是标准错误可以自动生成),错误代码(自填,或者标准错误填入GetLastError,或者WSAGetLastError),这样在出现错误的时候你就知道是哪个函数发生什么样的错误,便于调试。


/////////////////////////////////////////H////////////////////////////////////////////////////
#ifndef LG_EXCEPTION_H
#define LG_EXCEPTION_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//#include
#include

using namespace std;

namespace lglib
{
 /*!
  *异常处理的主要成员,是所有其他异常处理器的基类
  *的异常处理类,确保了整体的标准兼容。
  */
 class __LGLIB__ lg_Exception// : public exception
 {
 public:// 构造/析构
  lg_Exception(const string& Method, const string& Message, int ErrCode) throw();
  lg_Exception( const string& Method,DWORD ErrCode ) throw();
  virtual ~lg_Exception();
 public:
  /*****************************说明*************************************/
  //void foo() throw (int);      // 只能抛出int型异常
  //void bar() throw ();         // 不抛出任何异常
  //void baz();                  // 可以抛出任意类型的异常或者不抛出异常
  /**********************************************************************/
  const string getMethod( void ) const throw() { return m_szMethod; }
  const string getMessage( void ) const throw() { return m_szMessage; }
  const DWORD getErrCode( void ) const throw() { return m_nErrCode; }
  virtual const char* What( void ) const throw()
  {
   return getMessage().c_str();
  }
 protected: // 保护数据
  string m_szMethod;     // tell u witch function send this exception
  string m_szMessage;     // describe this exception
  uint32 m_nErrCode;     // code of exception
 private: // 私有方法
  void processErrCode( void ); // 处理错误代码 
 };
}

#endif
/////////////////////////////////////////CPP////////////////////////////////////////////////////

#include "stdafx.h"
#include "lg_Exception.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

using namespace std;

namespace lglib
{

 lg_Exception::lg_Exception( const string& Method, const string& Message, int ErrCode ) throw()
  : m_szMethod( Method ), m_szMessage( Message ), m_nErrCode( ErrCode )
 {
 }

  lg_Exception::lg_Exception( const string& Method,DWORD ErrCode ) throw()
   : m_szMethod( Method ), m_nErrCode( ErrCode )
 {
  processErrCode(); 
 }

 lg_Exception::~lg_Exception(void)
 {
 }
 
 void lg_Exception::processErrCode( void ) // 处理错误代码 
 {
  
  const uint32 nTempSize = 2048;
  char* szTemp = new char[ nTempSize ];
  
  // 格式化错误信息
  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, m_nErrCode,
   MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
   szTemp,
   nTempSize,
   NULL);
  m_szMessage = szTemp;
  delete [] szTemp;
 }
}

posted on 2006-04-29 23:19 longest 阅读(529)   


--------------------next---------------------

阅读(167) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~