C++处理异常程序小例子

异常处理的作用:
1、当异常发生时,能够将异常通知给编程人员或者用户
2、然后使本来已经中断的程序以适当的方式继续运行,或者退出。并且能够保存用户的当前操作,或者进行数据回稳
3、最后再把占用的资源释放掉

 

>>>>所有代码都是在win10+vs2017上运行<<<<

1、处理除数为0

#include "pch.h"
#include<cstdlib> #include<iostream> #include<string> using namespace std; class Error { public: virtual void showErrorMsg() = 0; }; class MyExecptionForZero : public Error { private: string errMsg; public: MyExecptionForZero(string str) : errMsg(str) {}; void showErrorMsg() { cout << errMsg << endl; } }; void myTeset(int i, int j) { MyExecptionForZero m("除数为0啦"); cout << "I want to get the result for j/i ..." << endl; if (i == 0) { throw m; } else { cout << j / i << endl; } } int main(int argc, char *argv[]) { int i = 0; int j = 4; try { myTeset(i, j); } catch (Error &e) { e.showErrorMsg(); } system("PAUSE"); return EXIT_SUCCESS; }

 

2、访问数组越界

#include "pch.h"
#include<iostream>
using namespace std;
enum index { underflow, overflow };
int array_index(int *a, int n, int index);

class error {
	virtual void showerrormsg() = 0;
};

int main()
{
	int *a = new int[10];
	for (int i = 0; i < 10; i++)
		a[i] = i;

	cout << a[100] << endl;
	try
	{
		cout << a[100] << endl;
		cout << array_index(a, 10, 5) << endl;
		cout << array_index(a, 10, -1) << endl;
		cout << array_index(a, 10, 15) << endl;
	}
	catch (index e)
	{
		if (e == underflow)
		{
			cout << "index underflow!" << endl;
			exit(-1);
		}
		if (e == overflow)
		{
			cout << "index overflow!" << endl;
			exit(-1);
		}
	}
	return 0;
}
int array_index(int *a, int n, int index)
{
	if (index < 0) throw underflow;
	if (index > n - 1) throw overflow;
	return a[index];
}

  

3、string类的at()函数,访问字符串越界

#include "pch.h"
#include<iostream>
#include <string>
#include <exception>
using namespace std;

int main() {
	string Variable("zhang");
	cout << Variable << endl;
	char temp = 'a';
	try {
		temp = Variable.at(9);
	}
	catch (const exception &zhc) {
		cout << "error:" << zhc.what() << endl;
	}
	cout << temp << endl;
	return 0;
}

  

4、catch在匹配过程中的类型转换

C/C++中存在多种多样的类型转换,以普通函数(非模板函数)为例,发生函数调用时,如果实参和形参的类型不是严格匹配,那么会将实参类型进行适当的转换,以适应形参的类型,这些转换包括:

  • 算数转型:例如int转换为float,char转换为int,double转换为int等;
  • 向上转型:也就是派生类向基类的转换
  • const转换:也就是非const类型转换为const类型,例如将char* 转换为 const char*
  • 数组或函数指针转换:如果函数形参不是引用类型,那么数组名会转换为数组指针,函数名也会转换为函数指针。
  • 用户自定的类型转换

catch在匹配异常类型的过程中,也会进行类型转换,但是这种转换受到了更多的限制,仅能进行「向上转型」、「const 转换」和「数组或函数指针转换」,其他的都不能应用于 catch。

(1) 向上转型的实例代码:

#include "pch.h"
#include<iostream>
#include <string>
#include <exception>
using namespace std;

class Base {};
class Derived : public Base {};

int main() {
	try {
		throw Derived();
		cout << "This statement will not be executed" << endl;
	}
	catch (int) {
		cout << "Execption type:int" << endl;
	}
	catch (char *) {
		cout << "Execption type:char *" << endl;
	}
	catch (Base) {
		cout << "Exception type:Base" << endl;
	}
	catch (Derived) {
		cout << "Exception type:Derived" << endl;
	}

	return 0;
}

 

(2) 数组转指针,非const指针转换为const指针

#include "pch.h"
#include<iostream>
#include <string>
#include <exception>
using namespace std;

int main() {
	int nums[] = { 1, 2, 3 };
	try {
		throw nums;
		cout << "This statement will not be executed." << endl;
	}
	catch (const int *) {
		cout << "Exception type: const int *" << endl;
	}
	return 0;
}

  

posted @ 2020-01-15 16:04  世纪强风  阅读(495)  评论(0编辑  收藏  举报