您的位置:首页 > 编程语言 > C语言/C++

c++:-9

2022-05-21 17:02 3139 查看

上节(c++:-8)主要学习了C的流类库和输入输出,本节学习C的异常处理。

##异常处理 ###介绍 (1)异常处理的基本思想: (2)异常处理的语法: (3)举例:处理除0异常

#include <iostream>
using namespace std;
int divide(int x, int y) {
if (y == 0)
throw x; //抛出异常
return x / y;
}
int main() {
try {
cout << "5 / 2 = " << divide(5, 2) << endl;
cout << "8 / 0 = " << divide(8, 0) << endl;
cout << "7 / 1 = " << divide(7, 1) << endl;
} catch (int e) { //捕获异常
cout << e << " is divided by zero!" << endl;
}
cout << "That is ok." << endl;
return 0;
}
输出:
5 / 2 = 2
8 / 0 = 8 is divided by zero!
That is ok.

###异常接口声明

  • 一个函数显式声明可能抛出的异常,有利于函数的调用者为异常处理做好准备
  • 可以在函数的声明中列出这个函数可能抛掷的所有异常类型,例如:
void fun() throw(A,B,C,D);
  • 若无异常接口声明,则此函数可以抛掷任何类型的异常。
  • 不抛掷任何类型异常的函数声明如下:
void fun() throw();

###异常中的析构函数

异常发生时,是自动析构的:

(1)找到一个匹配的catch异常处理后

  • 初始化异常参数。
  • 将从对应的try块开始到异常被抛掷处之间构造(且尚未析构)的所有自动对象进行析构。

(2)从最后一个catch处理之后开始恢复执行。

下面给出例子: (1)try块中子函数若抛出异常,则处理异常与局部对象析构的先后顺序是:先抛出异常,再析构子函数中的局部对象 (2)带析构语义的类的C++异常处理

#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 << "Throw MyException in func()" << endl;
throw MyException("exception thrown by func()");
}

int main() {
cout << "In main function" << endl;
try {
func();
} catch (MyException& e) {
cout << "Caught an exception: " << e.getMessage() << endl;
}
cout << "Resume the execution of main()" << endl;
return 0;
}
输出:
In main function
Constructor of Demo
Throw MyException in func()
Destructor of Demo
Caught an exception: exception thrown by func()
Resume the execution of main()

##异常标准库 (1)标准异常类的继承关系: (2)C++标准库各种异常类所代表的异常: (3)标准异常类的基础

  • exception:标准程序库异常类的公共基类
  • logic_error表示可以在程序中被预先检测到的异常 如果小心地编写程序,这类异常能够避免
  • runtime_error表示难以被预先检测的异常 (4)举例:三角形面积计算

编写一个计算三角形面积的函数,函数的参数为三角形三边边长a、b、c,可以用Heron公式计算

#include <iostream>
#include <cmath>
#include <stdexcept>
using namespace std;

//给出三角形三边长,计算三角形面积
double area(double a, double b, double c)  throw (invalid_argument)//invalid_argument是异常类的对象
{
//判断三角形边长是否为正
if (a <= 0 || b <= 0 || c <= 0)
throw invalid_argument("the side length should be positive");//处理该异常完后就结束,下面不会执行了
//判断三边长是否满足三角不等式
if (a + b <= c || b + c <= a || c + a <= b)
throw invalid_argument("the side length should fit the triangle inequation");
//由Heron公式计算三角形面积
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 {
double s = area(a, b, c);   //尝试计算三角形面积
cout << "Area: " << s << endl;
} catch (exception &e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}

#include <iostream>
using namespace std;

class CException
{
public:
CException() {}
virtual ~CException() {}
void Reason() {cout << "CException" << endl;}
};
void fn1()
{
throw CException();
}
int main()
{
try {
fn1();
} catch (CException& ce) {//捕获异常对象
ce.Reason();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: