自定义异常处理

来源:互联网 发布:js 根据日期计算年龄 编辑:程序博客网 时间:2024/05/16 23:57

/************************************************************************/
/*
File: exception.h
Author: WLW
Data: 2006.4.24
Purpose: Exception deal
*/
/************************************************************************/
#pragma once
#include <Windows.h>
#include <string>
using namespace std;

class Exception
{
public:
 Exception(void){ strError = "Unknown Exception!/r/n"; }
 Exception(const Exception &e) { strError = e.strError; }
 Exception(const char *pcszError){ strError = pcszError; }
 ~Exception(void) {}
 void ThrowException(void) const { OutputDebugString(strError.c_str()); }
 void ThrowException(const char *pcszError) const { OutputDebugString(pcszError); }
 const string Description(void) const { return strError; }
private:
 string strError;
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int _tmain(int argc, _TCHAR* argv[])
{
 try
 {
  int i = 3;
  if (i < 4)
  {
   throw Exception("less number/r/n");
  }
 }
 catch (Exception e)
 {
  e.ThrowException();
  return 0;
 }
 return 1;
}

原创粉丝点击