C++異常的幾種捕獲方式


捕獲指定的類型

這樣的話可以對每種異常做出不同的處理,例如:

#include <iostream>

using namespace std;

void A(int n){
	int a = 1;
	float b = 0.2;
	double c = 0.3;

	if(n == 1)
		throw a;
	else if(n == 2)
		throw b;
	else if(n == 3)
		throw c;
}

int main()
{
	int test;

	while(cin >> test){
		try{
			A(test);
		}catch(int i){
			cout << "Catch int exception " << i << endl;
		}catch(float i){
			cout << "Catch float exception " << i << endl;
		}catch(double i){
			cout << "Catch double exception " << i << endl;
		}
}

	return 0;
}

捕獲泛型

如果想捕獲全部類型異常的話,C++ 提供了一種簡便的做法,在 catch 子句的異常聲明中使用省略號來作為異常聲明,例如:

void function(){  
    try {  
        /* your code */  
    }catch(...) {  
        /* handler */
    }
}

捕獲類

例如:

#include <iostream>

using namespace std;

class Base{
public:
    void name(){
        cout << "My name is Base" << endl;
    }
};

void A(){
    throw Base();
}

int main()
{
    try{
        A();
    }catch(Base &e){
        e.name();
    }

    return 0;
}

也可以捕獲 Base 的子類,並且在 Base 類的成員函數前加 virtual 實現多態,這樣的話就可以調用子類的 name 方法,例如:

#include <iostream>

using namespace std;

class Base{
public:
    virtual void name(){
        cout << "My name is Base" << endl;
    }
};

class Derived : public Base{
public:
    void name(){
        cout << "My name is Derived" << endl;
    }
};

void A(){
    throw Derived();
}

int main()
{
    try{
        A();
    }catch(Base &e){
        e.name();
    }

    return 0;
}

捕獲未期望的異常

可以在函數名后用 throw 來聲明該函數可能拋出的異常,例如:

#include <iostream>

using namespace std;

void A() throw (int, float)
{
    int a = 1;
    float b = 0.2;
    double c = 0.3;

    throw c;
}

int main()
{
    try{
        A();
    }catch(...){
        cout << "Catch exception" << endl;
    }

    return 0;
}

但是,如果函數拋出的異常類型未在聲明范圍內的話,程序就會發生崩潰:

運行結果:

terminate called after throwing an instance of 'double'
Aborted (core dumped)

即使你使用了 catch(...) ,但它也捕獲不到,異常會繼續向上匯報,最終被系統默認的函數進行處理。

但我們可以使用 set_unexpected (unexpected_handler func) 這個函數來修改默認的處理方法,來實現自己的處理方式。

未實現捕獲的異常

假如函數拋出一個 double 的異常,但在我們捕獲的函數中沒有實現 double 類型的捕獲,當然也沒有使用 catch(...),這種情況和未期望的異常差不多,它也會上報系統,調用系統默認的處理函數。同樣我們也可以更改這個默認函數,使用如下方法:

terminate_handler set_terminate (terminate_handler func)

示例程序:

#include <iostream>

void exception(){
    std::cout << "my_terminate" << std::endl;
}

int main()
{
    std::set_terminate(exception);

    throw 0;

    return 0;
}

運行結果:

my_terminate
Aborted (core dumped)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM