8.25C++:异常处理基本思想、异常处理中的构造与析构、标准程序库异常处理

1 异常处理基本思想

在这里插入图片描述

在这里插入图片描述
①处理除0异常,异常发生后续语句不执行

#include <iostream>
using namespace std;
int divede(int x, int y) {
    if (y == 0)
        throw x; //抛出异常
    return x / y;
}
int main()
{
    try { //可能发生异常的调用写在try里
        cout << "5 / 2 = " << divede(5, 2) << endl;
        cout << "3 / 0 = " << divede(3, 0) << endl;
        cout << "7 / 3 = " << divede(7, 3) << endl;
    }
    catch (int e) {  //捕获异常,此处类似函数实参传至形参
        cout <<  e << " cannot be divided by 0" << endl; //发生异常的处理方式
    }
    return 0;
}
输出:
5 / 2 = 2
3 cannot be divided by 0

在这里插入图片描述
2 异常处理中的构造与析构

在这里插入图片描述

#include <iostream>
#include <string>
using namespace std;

class MyException {  //异常处理类
public:
	MyException(const string& message) : message(message) {};
	~MyException() {}
	const string& getMessage() const {
		return message;
	}
private:
	string message;
};

class Demo {
public:
	Demo() {
		cout << "Constructor of Demo" << endl;
	}
	~Demo() {
		cout << "Destructor of Demo" << endl;
	}
};

void func() throw(MyException) {
	Demo d; //构造
	cout << "接下来抛掷异常" << endl;
	throw MyException("exception thrown bt func");
}

int main() {
	try {
		func(); //func可能发生异常,放到try中
	}  //先结束try语句,再catch,因此try中构造的Demo对象d先析构
	catch (MyException& e) { //异常对象为类的对象引用
		cout << "Caught an exception:" << e.getMessage() << endl;
	}
	cout << "main contunue" << endl;
	return 0;
}
输出:
Constructor of Demo
接下来抛掷异常
Destructor of Demo
Caught an exception:exception thrown bt func
main contunue

3 标准程序库异常处理

异常类继承关系
基类:exception

在这里插入图片描述
各种异常类所代表的异常
在这里插入图片描述
在这里插入图片描述

#include <iostream>
#include <string>
#include <stdexcept>
#include <cmath> //数学常用库
using namespace std;
double area(double a, double b, double c) throw(invalid_argument){  //throw中写可能发生的异常类型
	if (a <= 0 || b <= 0 || c <= 0)
		throw  invalid_argument("the side length should be positive");//用"xxx"初始化一个invalid_argument对象
	if (a + b <= c || a + c <= b || b + c <= a)
		throw invalid_argument("the side length should fit the triangle inequation");
	double s = (a + b + c) / 2;
	return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
	double a, b, c;
	cout << "Please input the side lengths of a triangle: ";
	cin >> a >> b >> c;
	try {
		cout << "Area:" << area(a, b, c) << endl;
	}
	catch (exception &e){  //统一用基类对象
		cout << "Error:" << e.what() << endl; //what()函数可以调用输入的错误信息
	}
	return 0;
}
输入输出1:
Please input the side lengths of a triangle: -1 4 6
Error:the side length should be positive
输入输出2:
Please input the side lengths of a triangle: 1 2 3
Error:the side length should fit the triangle inequation
输入输出3:
Please input the side lengths of a triangle: 3 4 5
Area:6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值