65 c 異常處理(下)

2021-08-19 22:01:57 字數 4017 閱讀 3587

catch語句塊中可以丟擲異常:

trycatch(int i)  -->將捕獲的異常重寫丟擲

catch(...)  //沒有名字,直接丟擲

catch中丟擲的異常需要外層try...catch...捕獲

為啥要重新丟擲異常:catch中捕獲的異常可以被重新解釋後丟擲。

工程開發中使用這樣的方式統一異常型別:

第三方庫(void func(int i);//異常型別為int*)<----私有庫(void myfunc(int i) //異常型別為exception)

工程開發:通過呼叫myfunc獲得func函式的功能和統一的異常資訊--->呼叫私有庫

如果呼叫私有庫沒有的功能,要呼叫第三方庫,myfunc用來封裝直接呼叫第三方庫中的函式,在myfunc捕獲func丟擲的異常,重新解釋為我們自己的異常,然後丟擲我們自己的異常,用catch丟擲異常統一異常的型別。

#include

#include

using namespace std;

void demo()

catch(int i)

catch(...)

}catch(...)

}/*下邊的func

假設: 當前的函式式第三方庫中的函式,因此,我們無法修改源**

函式名: void func(int i)

丟擲異常的型別: int

-1 ==》 引數異常

-2 ==》 執行異常

-3 ==》 超時異常

*/void func(int i)

if( i > 100 )

if( i == 11 )

cout << "run func..." << endl;

//我們自己的函式重新解釋func

void myfunc(int i)

catch(int i)}}

int main(int argc, char *argv)

catch(const char* cs)     //上邊的 「timeout exception」

return 0;

}異常處理與函式的區別:函式處理會有預設的型別轉換,而異常處理沒有型別轉換,嚴格匹配。

2、異常的型別可以是自定義類型別

對於類型別異常的,匹配依舊是至上而下嚴格匹配。賦值相容性原則在異常匹配中依然適用。

一般而言,匹配子類異常的catch放在上部,匹配父類異常的catch放在下部。子類異常物件可以被父類catch語句抓住。

在工程中會定義一系列的異常類,每個類代表工程中可能出現的一種異常型別,**復用時可能需要重解釋不同的異常類,在定義catch語句塊時推薦使用引用作為引數(類物件可以避開拷貝構造,提高效率)。

#include

#include

using namespace std;

class base

;class exception : public base

int id() const

string description() const

};/*

假設: 當前的函式式第三方庫中的函式,因此,我們無法修改源**

函式名: void func(int i)

丟擲異常的型別: int

-1 ==》 引數異常

-2 ==》 執行異常

-3 ==》 超時異常

*/void func(int i)

if( i > 100 )

if( i == 11 )

cout << "run func..." << endl;

}void myfunc(int i)

catch(int i)}}

int main(int argc, char *argv)

catch(const exception& e)  //有const ,成員函式也必須是const成員函式

catch(const base& e)  //exception物件型別是base物件的子類,子類物件可以當作父類物件使用,必須放到下邊

return 0;

}3、c++標準庫中提供了實用異常類族,標準庫中的異常都是從exception類派生的,exception類有兩個主要的分支

logic_error:常用於程式中的可避免邏輯錯誤。

runtime_error:常用於程式中無法避免的惡性錯誤。

陣列類優化:

#ifndef _array_h_

#define _array_h_

#include //標準庫檔案

using namespace std;

template

< typename t, int n >

class array

return ret;

template

< typename t, int n >

bool array::get(int index, t& value)

return ret;

//以下優化

template

< typename t, int n >

t& array::operator (int index)

else

template

< typename t, int n >

t array::operator (int index) const

else

template

< typename t, int n >

array::~array()

#endif

#ifndef _heaparray_h_

#define _heaparray_h_

#include

using namespace std;

template

< typename t >

class heaparray

return ret;

template

< typename t >

int heaparray::length() const

template

< typename t >

bool heaparray::get(int index, t& value)

return ret;

}template

< typename t >

bool heaparray::set(int index, t value)

return ret;

}//優化

template

< typename t >

t& heaparray::operator (int index)

else

}template

< typename t >

t heaparray::operator (int index) const

else

}template

< typename t >

heaparray& heaparray::self()

template

< typename t >

const heaparray& heaparray::self() const

template

< typename t >

heaparray::~heaparray()

#endif

#include

#include

#include "array.h"

#include "heaparray.h"

using namespace std;

void testarray()

for(int i=0; i}    

delete pa;

}int main(int argc, char *argv)

catch(...)

return 0;

}catch語句塊中可以丟擲異常,異常型別可以是自定義型別,賦值相容性原則在異常匹配中依然適用,標準庫的異常都是從exception類派生的。

65 C 檔案讀寫

寫檔案 ofstream ofs open 指定開啟方式 isopen 判斷是否開啟成功 ofs 資料 ofs.close 讀操作 ifstream ifs 指定開啟方式 ios in isopen判斷是否開啟成功 三種方式讀取資料 define crt secure no warnings inc...

異常處理(下)

異常與物件 工程中的異常應用 函式級try語法 小結問題 有時在工程中只關心是否產生 了異常,而不關心具體的異常型別,c 語言可以做到嗎?c 中的 catch語句可以使用 捕獲所有的異常 include include using namespace std int test int i if i ...

Linux 下 C 異常處理技巧

處理固有語言侷限性的四種技術 sachin o.agrawal sachin agrawal in.ibm.com 高階軟體工程師,ibm software labs,india 簡介 處理 c 中的異常會在語言級別上遇到少許隱含限制,但在某些情況下,您可以繞過它們。學習各種利用異常的方法,您就可以...