目录
一MFC异常二CPP异常三类的异常面向对象的异常四异常类的派生五虚函数的异常六模板类的异常七内存异常
【目录】
一、 MFC异常: 2 二、 CPP异常: 3 三、 类的异常(面向对象的异常): 4 1、 例子1: 4 2、 例子2: 5 四、 异常类的派生: 7 五、 虚函数的异常: 8 六、 模板类的异常: 10 七、 内存异常 11
一、MFC异常:
#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()
{
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)
{
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(),
0))
{
_tprintf(_T(
"错误: MFC 初始化失败\n"));
nRetCode =
1;
}
else
{
openfile();
system(
"pause");
}
}
else
{
_tprintf(_T(
"错误: GetModuleHandle 失败\n"));
nRetCode =
1;
}
return nRetCode;
}
二、CPP异常:
#include <iostream>
#include <string.h>
using namespace std;
class wrong
{
};
int intdiv(
int a,
int b)
{
try
{
if (b==
0)
{
throw 10;
abort();
}
int c = a / b;
return c;
}
catch (
int data )
{
cout <<
"除法异常已经处理";
return -
1;
}
}
int intdivA(
int a,
int b)
{
return a / b;
}
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;
}
}
std::
cout << intdiv(x, y);
cin.get();
cin.get();
}
三、类的异常(面向对象的异常):
1、例子1:
#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];
for (
int i =
0; i < num;i++)
{
p[i] =
0;
}
}
int &
operator[](
int num)
{
if (num <
0 || num>= n)
{
throw wrongA();
}
return p[num];
}
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 };
getchar();
}
2、例子2:
#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>
#include <string>
using namespace std;
class box
{
public:
box(
int data)
{
cout <<
"开始构造";
if (data ==
0)
{
zero z1(
22);
z1.seterror(
21);
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 wrong{};
class big{};
class small{};
class zero :
public small
{
public:
int errorcode;
zero(
int i) :errorcode(i)
{ }
void seterror(
int i)
{
errorcode = i;
}
};
private:
int a;
};
void main()
{
try
{
box newbox(
0);
}
catch (box::wrong)
{
cout <<
"正方体长度异常";
}
catch (box::big)
{
cout <<
"正方体长度太长";
}
catch (box::zero w)
{
if (w.errorcode ==
22)
{
cout <<
"22号错误正方体长度不可以为0";
}
else
{
cout <<
"fei22号错误正方体长度不可以为0";
}
}
catch (box::small)
{
cout <<
"正方体长度taiduan";
}
cin.get();
}
五、虚函数的异常:
#include<iostream>
#include <string>
using namespace std;
class box
{
public:
box(
int data)
{
cout <<
"开始构造";
if (data ==
0)
{
zero z1(
22);
z1.seterror(
21);
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 wrong
{
public:
virtual void show()
{
cout <<
"wrong" << endl;
}
};
class big:
public wrong
{
public:
void show()
{
cout <<
"big wrong" << endl;
}
};
class small:
public wrong
{
public:
void show()
{
cout <<
"small wrong" << endl;
}
};
class zero :
public small
{
public:
int errorcode;
zero(
int i) :errorcode(i)
{ }
void seterror(
int i)
{
errorcode = i;
}
};
private:
int a;
};
void main()
{
try
{
box newbox(
11168);
}
catch (box::zero w)
{
if (w.errorcode ==
22)
{
cout <<
"22号错误正方体长度不可以为0";
}
else
{
cout <<
"fei22号错误正方体长度不可以为0";
}
}
catch (box::wrong &wrong1)
{
wrong1.show();
}
cin.get();
}
六、模板类的异常:
#include<iostream>
using namespace std;
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;
}
}
private:
int size;
T *p;
};
void main()
{
try
{
Array<
double> mya(
1);
}
catch (Array<
double>::wrong & wrong1)
{
wrong1.show();
}
cin.get();
}
七、内存异常
#include<iostream>
#include<string>
using namespace std;
class stduent
{
public:
stduent()
{
cout <<
"构造" << endl;
}
~stduent()
{
cout <<
"析构" << endl;
}
};
class X
{
public:
void *p;
char str[
30];
X(
void *pt):p(pt)
{ }
};
bool quit =
false;
void run()
{
stduent *p =
new stduent;
if (p!=
nullptr)
{
quit =
true;
if (quit ==
true)
{
throw X(
reinterpret_cast<
void *>(p));
}
}
}
void main()
{
try
{
run();
}
catch (X e)
{
cout <<
"内存异常,内存泄漏"<<e.p;
}
cin.get();
}