網站首頁 編程語言 正文
一、異常的引入
傳統的C語言處理異常的方式有兩種:
1.終止程序:使用assert斷言語句,如果發生內存錯誤等,比如內存泄漏或者除0錯誤,都會直接終止程序。
2.返回錯誤碼:通過錯誤碼判斷發生的異常的類型是什么,如系統的很多庫的接口程序通過把錯誤碼放到errno中,表示錯誤。
在實際中的C語言程序基本都是通過返回錯誤碼的方式來處理錯誤的,部分情況下使用終止程序來處理比較嚴重的錯誤。
二、C++異常的關鍵字
目前市面上的大部分的主流語言都是使用異常機制來處理錯誤的,當一個函數發現自己無法處理一些錯誤的時候會進行拋異常處理,讓函數直接跳轉到捕獲異常的地方去處理異常。
下面介紹C++處理異常的幾個關鍵字:
throw:當問題出現的時候,程序會拋出一個異常,這是通過throw關鍵字來完成的。
catch:通過異常處捕獲異常,catch塊主要用于處理異常,或者執行一些其他的操作。
try:try塊中的代碼標識將被激活的特定異常,它的后面一般會跟一個catch塊。
三、異常的拋出與處理規則
double Disvision(int a, int b)
{
if (b == 0)
{
throw "Disvision by zero condition";
}
else
{
return ((double)a / (double)b);
}
}
void Func()
{
int len, time;
cin >> len >> time;
cout << Disvision(len, time) << endl;
}
int main()
{
try
{
Func();
}
catch(const char* errmsg)
{
cout << errmsg << endl;
}
catch (...)
{
cout << "unkown exception" << endl;
}
return 0;
}
當向time傳入的值為0的時候,調用throw拋出異常,catch會捕獲到該異常進行處理。
1.異常是通過拋出對象而引發的,該對象的類型決定了應該激活哪一個catch的處理代碼。
2.被選中的處理代碼是調用鏈中與該對象類型匹配且離拋出異常位置最近的一個。
在這段程序中,有一個調用鏈:main()->Func()->Disvision(),在Disvision中拋出異常就會沿著調用鏈尋找能捕獲異常的catch來執行。假設Func()中有一個與main()中一模一樣的catch函數,那么除0的異常就會優先調用Func()中的catch,因為它離著最近。
3.catch的異常其實是拋出對象的拷貝,因為真正拋出的對象在出作用域的時候就已經被銷毀了,拷貝的對象在catch成功之后也會自動銷毀。
4.catch(…)可以捕獲任意類型的異常,但問題是不知道異常錯誤是什么。
5.實際中拋出和捕獲的匹配原則有一個例外,并不都是類型完全匹配,可以拋出的派生類對象,使用基類捕獲。(要引入多態)
6.當catch異常之后,會沿著catch后的子句繼續執行,類似goto,算是異常的一個缺陷。
四、異常缺陷的處理
double Disvision(int a, int b)
{
if (b == 0)
{
throw "Disvision by zero condition";
}
else
{
return ((double)a / (double)b);
}
}
void Func()
{
int* array = new int[10];
int len, time;
cin >> len >> time;
cout << Disvision(len, time) << endl;
delete[] array;//一旦拋出異常,這里就不會被執行
}
int main()
{
try
{
Func();
}
catch(const char* errmsg)
{
cout << errmsg << endl;
}
catch (...)
{
cout << "unkown exception" << endl;
}
return 0;
}
對于這段代碼而言,一旦Division拋出異常就會立刻執行主函數中的catch,delete釋放內存就不會被執行。因此我們需要對這種情況進行處理,即在Func中也捕獲一次異常,但是不對異常進行處理,只是釋放空間:
void Func()
{
int* array = new int[10];
int len, time;
cin >> len >> time;
try {
cout << Disvision(len, time) << endl;
}
catch (const char* errmsg)
{
cout << "釋放空間" << endl;
delete[] array;
throw;
}
}
在釋放空間之后,直接調用throw表示將捕獲到的異常再拋出去。
五、自定義異常體系
class Exception
{
public:
Exception(const string& errmsg, int id)
:_errmsg(errmsg)
, _id(id)
{}
virtual string what() const
{
return _errmsg;
}
protected:
string _errmsg;
int _id;
};
class SqlException : public Exception
{
public:
SqlException(const string& errmsg, int id, const string& sql)
:Exception(errmsg, id)
, _sql(sql)
{}
virtual string what() const
{
string str = "SqlException:";
str += _errmsg;
str += "->";
str += _sql;
return str;
}
private:
const string _sql;
};
class CacheException : public Exception
{
public:
CacheException(const string& errmsg, int id)
:Exception(errmsg, id)
{}
virtual string what() const
{
string str = "CacheException:";
str += _errmsg;
return str;
}
};
class HttpServerException : public Exception
{
public:
HttpServerException(const string& errmsg, int id, const string& type)
:Exception(errmsg, id)
, _type(type)
{}
virtual string what() const
{
string str = "HttpServerException:";
str += _type;
str += ":";
str += _errmsg;
return str;
}
private:
const string _type;
};
void SQLMgr()
{
srand(time(0));
if (rand() % 7 == 0)
{
throw SqlException("權限不足", 100, "select * from name = '張三'");
}
//throw "xxxxxx";
}
void CacheMgr()
{
srand(time(0));
if (rand() % 5 == 0)
{
throw CacheException("權限不足", 100);
}
else if (rand() % 6 == 0)
{
throw CacheException("數據不存在", 101);
}
SQLMgr();
}
void HttpServer()
{
// ...
srand(time(0));
if (rand() % 3 == 0)
{
throw HttpServerException("請求資源不存在", 100, "get");
}
else if (rand() % 4 == 0)
{
throw HttpServerException("權限不足", 101, "post");
}
CacheMgr();
}
void ServerStart()
{
while (1)
{
this_thread::sleep_for(chrono::seconds(1));//休眠1s
try {
HttpServer();
}
catch (const Exception& e) // 這里捕獲父類對象就可以
{
// 多態
cout << e.what() << endl;
}
catch (...)
{
cout << "Unkown Exception" << endl;
}
}
}
int main()
{
ServerStart();
return 0;
}
這里使用隨機數進行模擬異常的拋出類型,我們使用父類捕獲異常,拋出子類異常,使用多態調用子類中的what()函數。
六、異常規范
1.異常規格說明的目的是為了讓函數使用者知道。可以在函數的后面接throw類(類型),列出這個函數可能拋的所有異常類型。
2.函數后面接throw(),表示函數不拋異常。
3.若無異常接口聲明,則此函數可以拋擲任何類型的異常。
void func() throw(A, B, C, D);//只會拋A/B/C/D中的某種類型的異常
void* operator new(size_t size) throw(bad alloc);//這里表示這個函數只會拋出bad_alloc的異常
void* operator new(size_t size, void* ptr) throw();//這個函數不會拋異常
在C++11中,引入了noexception
bool Compare(int x, int y) noexcept(noexcept(x > y)) //C++11
{
return x > y;//表示如果x > y不發生異常,則Compare函數不會發生異常。
}
七、異常安全
構造函數完成對象的初始化,最好不要在構造函數中拋異常,否則可能導致對象不完整,或沒有完全初始化。
析構函數主要完成資源的清理,最好不要在析構函數中拋異常,否則可能導致資源泄漏(內存泄漏,句柄未關閉)。
C++異常經常會導致資源泄漏的問題,比如在new和delete中拋出了異常,導致內存泄漏,在lock和unlock之間拋出了異常導致死鎖,C++經常使用RAII來解決以上問題,關于RAII我們在智能指針中講解。
八、異常的優缺點
1.優點
1.相比錯誤碼,更加清晰展示出錯信息。
2.很多庫中包含異常,boost,gtest,gmock等常用的庫,使用它們也需要異常。
3.部分函數使用異常更好處理,比如構造函數沒有返回值,不方便使用錯誤碼方式處理,比如T&operator這樣的函數,只有一個返回值,如果pos越界了只能使用異常或者終止程序處理,沒辦法通過返回值表示錯誤。
2.缺點
1.異常類似goto,會導致程序的執行流亂跳,并且非常混亂。
2.C++沒有垃圾回收機制,可能導致內存泄漏。
3.各個公司的異常體系不同,有一定的學習成本。
原文鏈接:https://blog.csdn.net/qq_51492202/article/details/127006781
相關推薦
- 2022-07-08 C語言完整實現12種排序算法(小結)_C 語言
- 2022-04-18 將stylus 樣式 轉換成sass,scss
- 2022-05-14 Python實現屏幕代碼雨效果的示例代碼_python
- 2022-09-04 c++?求數組最大最小值函數的實現_C 語言
- 2023-02-18 go?micro微服務框架項目搭建方法_Golang
- 2022-05-23 iOS實現無限滑動效果_IOS
- 2022-05-09 PyTorch實現線性回歸詳細過程_python
- 2023-03-25 Go語言基于viper實現apollo多實例快速_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支