C++异常处理机制

来源:互联网 发布:It 门槛 编辑:程序博客网 时间:2024/06/16 13:30

基础穿插语法:

       C++类中嵌套内的定义:

              下面直接上源码加这个源码的解说,秒懂.

// Test.cpp : 定义控制台应用程序的入口点。
//

//自己测试下类嵌类
#include "stdafx.h"
#include <iostream>
using namespace std;
class Big
{
public:
Big(){};
~Big(){};
int b;


class Small
{
public:
Small(){};
~Small(){};
int s;
};
int a;
};

int _tmain(int argc, _TCHAR* argv[])
{
Big c;
c.a;
c.b;

cout << sizeof(c);                 //测试大小为8个字节,并且Big对象也只能使用自己的成员.

Big::Small m;                       //使用Big内中定义的Small类定义对象.
m.s;


getchar();
return 0;
}


熟练理解与运用异常机制思想是:逻辑判断异常时候throw抛出,然后对代码片段进行保护的地方用try和catch包裹起来即可.

分类概况:

       1.CPP异常

       2.MFC异常

       3.类的异常

       4.类的异常加强

       5.模板类异常


1.CPP异常:

#include <iostream>
#include <string.h>
using namespace std;
//标识错误的类型
class wrong  
{
public:
wrong()
{
a = 10; b = 20;
};
int a;
int b;
};
int intdiv(int a, int b)
{
try
{
if (b==0)
{
throw 10;//可以是任何对象  wrong();
}
int c = a / b;
return c;
}
catch (int data )//类型名
{
cout << "除法异常已经处理";
return -1;
}
}
void main()
{
int x, y;
cin >> x >> y;
/*
try
{
if (y==0)
{
throw "被除数为0";
}
else if (x==0)
{
throw "除数为0";
}
}
catch (const char * s)
{
if (strcmp(s,"被除数为0")==0)
{
cout << "被除数为0异常,请重新输入";
cin >> x >> y;


}
else  if (strcmp(s, "除数为0") == 0)
{
cout << "除数为0异常,请重新输入";
cin >> x >> y;
}


}
*/
try
{
if (y == 0)
{
throw wrong();
}
else if (x == 0)
{
throw wrong();
}
}
catch (wrong m)
{
cout << m.a;


}
cin.get();
}


2.MFC异常

// MFC异常.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include "MFC异常.h"
#include<iostream>
#include <stdlib.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
void openfile()
{
//CFile file(_T("C:\\123.txt"), CFile::modeRead);
TRY
{
CFile file(_T("C:\\123.txt"), CFile::modeRead);//尝试动作,如果异常,抛出异常
}
CATCH(CFileException, e)//文件的异常
{
if (e->m_cause == CFileException::fileNotFound)
{
cout << "文件不存在,请认真检查";
}
}
END_CATCH
}




int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO:  更改错误代码以符合您的需要
_tprintf(_T("错误:  MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO:  在此处为应用程序的行为编写代码。
openfile();
system("pause");

}
}
else
{
// TODO:  更改错误代码以符合您的需要
_tprintf(_T("错误:  GetModuleHandle 失败\n"));
nRetCode = 1;
}
return nRetCode;
}

3.类的异常:

#include<iostream>
using namespace std;
class wrong
{






};
class wrongA
{






};
class Array
{
public:
Array(int num)
{
n = num;

if (num<=0)
{
throw  wrong();
}
p = new int[num];//正确代码在throw之后,不会被执行,
for (int i = 0; i < num;i++)
{
p[i] = 0;
}
}
int & operator[](int num)
{
if (num < 0 || num>= n)
{
throw wrongA();
}
return p[num];
}
protected:
private:
int *p;
int n;


};
void main()
{
try
{
Array myarrar(2);
myarrar[-1];
}
catch (wrongA  e)
{
cout << "下标越界";
}
catch (wrong  e)
{
cout << "程序发生异常,数组大小必须大于等于1";
}
cin.get();
}


void mainA()
{
int a[3] = { 1, 2, 3 };
// printf("%d", 2[a]);//*(2+a)
// printf("%d", a[9886655]);
getchar();
}


4.类的异常加强

#include<iostream>
#include <string>
using namespace std;
class box   //正方体
{
public:
box(int data) 
{
cout << "开始构造";


if (data ==0)
{
zero z1;
z1.errorcode = 212;
throw z1;

else if ( data >0 && data<100)
{
throw small();
}
else  if (data>10000)
{
throw big();
}
else if (data>100 && data<10000)
{
a = data;
}
else
{
throw wrong{};
}
}
    int  gettiji()
{
return  a*a*a;
}
class zero
{
public:
int errorcode;

}; 
class wrong{};
class big{};
class small{};
private:
int a;//变长


};


void main()
{
try
{
box newbox(0);
}
catch (box::zero w)
{
   if (w.errorcode==22)
   {
cout << "22号错误正方体长度不可以为0";
   } 
   else
   {
cout << "fei22号错误正方体长度不可以为0";
   }
}
catch (box::wrong)
{
cout << "正方体长度异常";
}
catch (box::big)
{
cout << "正方体长度太长";
}
catch (box::small)
{
cout << "正方体长度taiduan";
}
cin.get();
}


#include<iostream>


using namespace std;
//typename会明确类型


//模板的异常,处理通用数据类型,类中包含一个如何使用
//虚函数,虚函数可以指针,引用来实现。
//异常处理机制,一个接口处理通用的异常


template <class T>
class Array
{
public:
class wrong //模板类中定义一个异常类进行处理.这是通用类,并定义接口,供不同异常派生实现.
{
public:
virtual void show()
{
cout << "wrong"<< typeid(T).name();
}
};
class big :public wrong
{
public:
int x;
big(int n) :x(n)
{
}
void show()
{
cout << " big  wrong" << x << typeid(T).name();;
}
};
class small :public wrong
{
public:
int x;
small(int n) :x(n)
{


}
void show()
{
cout << " small  wrong" << x << typeid(T).name();;
}
};
Array(int n)
{
       if (n>0 && n<10)
       {
  throw small(n);
       } 
  else if (n>10000)
       {
  throw big(n);
       }
  else if (n<0)
  {
  throw wrong();
  }
  else
  {
  p = new T[n];
  size = n;
  }
}


protected:
private:
int size;
T *p;
};
void main()
{
try
{
Array<double> mya(1);
}
catch (Array<double>::wrong & wrong1)
{
wrong1.show();
}
cin.get();
}

0 0
原创粉丝点击