Exception handling in learning "Fundamentals of C + + Programming"

The idea and program realization of exception handling
The basic idea of exception handling

Syntax for exception handling

Handling the exception of division by zero

#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;
}

Exception interface declaration
A function explicitly declares the exceptions that may be thrown, which is helpful for the function caller to prepare for exception handling
You can list all the exception types that this function might throw in its declaration.

void fun() throw(A,B,C,D);

If there is no exception interface declaration, this function can throw any type of exception. A function that does not throw any type of exception is declared as follows:

void fun() throw();

Structure and analysis in exception handling
Automatic deconstruction
After a matching catch exception is found: initialize the exception parameter. All automatic objects constructed from the start of the corresponding try block to the place where the exception is thrown (and has not been deconstructed) are deconstructed.
Resume execution after the last catch processing.

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

Standard Library Exception Handling
Inheritance relationship of standard exception class

Exceptions represented by various exception classes in C + + standard library

Basis of standard exception class
Exception: common base class for standard library exception classes
Logic error refers to the exception that can be detected in the program in advance; if the program is written carefully, such exception can be avoided
Runtime error indicates an exception that is difficult to detect in advance
Write a function to calculate the area of a triangle. The parameters of the function are the length of three sides of a triangle a, b and c. you can use the Heron formula calculation:

//An invalid argument exception may be thrown
double area(double a, double b, double c) throw (invalid_argument) {
	//Judge whether the triangle side length is positive
	if (a <= 0 || b <= 0 || c <= 0)
		throw invalid_argument("the side length should be positive");
	//Judge whether the trilateral length satisfies the trigonometric inequality
	if (a + b <= c || b + c <= a || c + a <= b)
		throw invalid_argument("the side length should fit the triangle inequation");
	//Calculation of triangle area by Heron formula
	double s = (a + b + c) / 2;
	return sqrt(s*(s - a)*(s - b)*(s - c));
}
int main(){
	double a, b, c;//Triangle trilateral length
	cout << "Please input the side lengths of a triangle: ";
	cin >> a >> b >> c;
	try {
		double s = area(a, b, c);//Try to calculate triangle area
		cout << "Area: " << s << endl;
	}
	catch (exception &e) {
		cout << "Error: " << e.what() << endl;
	}
	return 0;
}

 

Posted by jobs on Tue, 17 Dec 2019 09:57:40 -0800