c++异常处理

最简单的异常处理,输出异常信息,abort终止整个程序运行

#include <iostream>
using namespace std;
double hmean(double a, double b)
{
	if (a == -b) {
		cout << "untenable arguments to hmean\n";
		abort();
	}

return 2.0 * a * b / (a + b);
}
int main() {
	cout << hmean(1,-1) << endl;
	cout << "hello world" << endl;
}

 改进一下,将函数的返回值作为bool变量进行判断,异常走异常处理,正常走正常处理,不终止程序的运行

#include <iostream>
using namespace std;
bool hmean(double a, double b, double* ans){
	if (a == -b) {
		*ans = DBL_MAX;
		return false;
	}
	else {
		*ans = 2.0 * a * b / (a + b);
	}
	return true;
}
int main(){
double x, y,z;
cout << "Enter two numbers";
while (cin >> x >> y){
	if (hmean(x, y, &z)) {
		cout << "Harmonic mean of" << x << "and " << y << "is" << z << endl;
		cout << "Enter next set of numbers<q to quit> : ";
	}//end of try block
	else {
		cout << "bad hmean arguments : a = -b not allowed"<<endl;
		cout << "Enter a new pairof numbers : ";
	//endofhandler
	}
	cout << "Bye!\n";
return 0;
}

如果异常处理太多,影响代码的可读性,不能清晰提炼出函数的主要逻辑,所以考虑将函数的主要逻辑和异常处理分开,将控制权从程序的一个部分转移到另一个部分

throw:抛出异常

catch:捕获异常

try:标识可能抛出异常的程序块

#include <iostream>
using namespace std;
double hmean(double a,double b) {
	if (a == -b) {
		throw "bad hmean() arguments:a = -b not allowed";
	}
	return 2.0 * a * b / (a + b);
}
int main() {
	double x, y, z;
	cout << "Enter twonumbers : ";
	while (cin >> x >> y) {
		try {	//startof tryblock
			z = hmean(x, y);
			cout << "Harmonic mean of" << x << "and" << y << " is " << z << endl;
			cout << "Enter next set of numbers<q to quit> :";	//endof tryblock
		}
		catch (const char* s) {	//startofexception handler
			cout << s << endl;
			cout << "Enter a new pair of numbers :";	//endofhandler
		}
	}
	cout << "Bye!\n";
	return 0;
}

抛出个const char*,catch参数也是const char*,匹配上了,但是,代码可读性不好

throw可以抛出任意类型,catch根据捕获的不同的类型,执行不同的异常处理

设计一个自定义类型

#include <iostream>
using namespace std;
class bad_hmean {
public:
	bad_hmean(double a = 0, double b = 0) :v1(a), v2(b) {};
	void mesg() {
		cout << "hmean(" << v1 << "," << v2 << ")" << "invalid arguments : a = -b\n";
	}
private:
	double v1;
	double v2;
};
double hmean(double x,double y) {
	if (x == -y) {
		throw bad_hmean(x, y);
	}
	return 2.0 * x * y / (x + y);
}
int main() {
	double x, y, z;
	cout << "Enter two numbers : ";
	while (cin >> x >> y) {
		try {	//startof tryblock
			z = hmean(x, y);
			cout << "Harmonic mean of " << x << " and " << y << " is " << z << endl;
			cout << "Enter next set of numbers<q to quit> :";	//endof tryblock
		}
		catch (bad_hmean& other) {	//startofexception handler
			other.mesg();
			cout << "Enter a new pair of numbers :";	//endofhandler
		}
	}
	cout << "Bye!\n";
	return 0;
}

继续改进,一个函数中,抛出多个异常,根据抛出的不同的类型执行不同的异常处理

#include <iostream>
using namespace std;
class bad_hmean {
public:
	bad_hmean(double a = 0, double b = 0) :v1(a), v2(b) {};
	void mesg() {
		cout << "hmean(" << v1 << "," << v2 << ")" << "invalid arguments : a = -b\n";
	}
private:
	double v1;
	double v2;
};
double hmean(double x,double y) {
	if (x == -y) {
		throw bad_hmean(x, y);
	}
	return 2.0 * x * y / (x + y);
}

class bad_gmean {
public:
	bad_gmean(double x = 0,double y = 0):v1(x),v2(y) {};
	void mesg() {
		cout << "These two args are invalid : x * y < 0" << endl;
	}
private:
	double v1;
	double v2;
};

double gmean(double x,double y) {
	if (x * y < 0) {
		throw bad_gmean(x, y);
	}
	return sqrt(x * y);
}
int main() {
	double x, y, z1,z2;
	cout << "Enter two numbers : ";
	while (cin >> x >> y) {
		try {	//startof tryblock
			z1 = hmean(x, y);
			cout << "Harmonic mean of " << x << " and " << y << " is " << z1 << endl;
			z2 = gmean(x, y);
			cout << "gmean of " << x << " and " << y << " is " << z2 << endl;
			cout << "Enter next set of numbers<q to quit> :";	//endof tryblock
		}
		catch (bad_hmean& other) {	//start of exception handler
			other.mesg();
			cout << "Enter a new pair of numbers :";	//endofhandler
		}
		catch (bad_gmean& other) {	//startofexception handler
			other.mesg();
			cout << "Enter a new pair of numbers :";	//endofhandler
		}
	}
	cout << "Bye!\n";
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码成行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值