10

xiaoxiao2021-02-28  112

目录 一MFC异常二CPP异常三类的异常面向对象的异常四异常类的派生五虚函数的异常六模板类的异常七内存异常

【目录】

一、 MFC异常: 2 二、 CPP异常: 3 三、 类的异常(面向对象的异常): 4 1、 例子1: 4 2、 例子2: 5 四、 异常类的派生: 7 五、 虚函数的异常: 8 六、 模板类的异常: 10 七、 内存异常 11

一、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; }

二、CPP异常:

#include <iostream> #include <string.h> using namespace std; /*标识错误的类型*/ class wrong { }; int intdiv(int a, int b) { try { if (b==0) { throw 10; //可以是任何对象 wrong(); abort();/*可以抛出这个异常*/ } int c = a / b; return c; } catch (int data )//类型名,一般是在throw抛出一个对象,这里接收异常对象 { 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];/*正确代码在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]; } 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) 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 /*两种错误的处理方式都接受,在处理的时候遇到任何一种都会处理,看catch的先后顺序执行哪个错误处理方式*/ { 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; //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; } } 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; //delete p; //p = nullptr; 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(); }
转载请注明原文地址: https://www.6miu.com/read-33884.html

最新回复(0)