上節(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;
}