我們在之前學(xué)習(xí)了 C++ 中有關(guān)異常的知識,現(xiàn)在我們來重新回顧下。那么異常的格式是什么呢?便是 try ... catch ...;try 語句處理正常的代碼邏輯,而 catch 語句則處理異常情況,try 語句中的異常由對應(yīng)的 catch 語句處理。格式如下
站在用戶的角度思考問題,與客戶深入溝通,找到富錦網(wǎng)站設(shè)計與富錦網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名申請、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋富錦地區(qū)。
try { double r = divide(1, 0); } catch(...) { cout << "Divided by zero ..." << endl; }
在 C++ 中,通過 throw 語句拋出異常信息。throw 拋出的異常必須被 catch 處理,當(dāng)前函數(shù)如果能處理異常,程序?qū)⒗^續(xù)往下執(zhí)行;如果當(dāng)前函數(shù)無法處理異常則函數(shù)停止執(zhí)行并返回。未被處理的異常會順著函數(shù)調(diào)用棧向上傳播,直到被處理為止,否則程序?qū)⑼V箞?zhí)行。如下
同一個 try 語句可以跟上多個 catch 語句。catch 語句可以定義具體處理的異常類型,不同類型的異常由不同的 catch 語句負(fù)責(zé)處理;try 語句中可以拋出任何類型的異常,catch(...) 用于處理所有類型的異常,任何異常都只能被捕獲(catch)一次。異常處理的匹配規(guī)則如下
異常的類型可以是自定義類類型,對于類類型異常的匹配依舊是至上而下嚴(yán)格匹配,賦值兼容性原則在異常匹配中依然適用。一般而言:匹配子類異常的 catch 放在上部,匹配父類異常的 catch 放在下部。現(xiàn)代 C++ 庫中必然包含充要的異常類族,因為異常類是數(shù)據(jù)結(jié)構(gòu)類所依賴的“基礎(chǔ)設(shè)施”!舉個例子,如果我們在申請內(nèi)存失敗時便要但會一個內(nèi)存不足的異常。異常類如下圖所示
下來我們來看看異常類功能定義,如下
下來我們來創(chuàng)建一個異常類族,代碼如下
Exception.h 源文件
#ifndef EXCEPTION_H#define EXCEPTION_H #include "Object.h" namespace DTLib { class Exception : public Object { private: char* m_message; char* m_location; void init(const char* message, const char* file, int line); public: Exception(const char* message); Exception(const char* file, int line); Exception(const char* message, const char* file, int line); Exception(const Exception& e); Exception& operator= (const Exception& e); virtual const char* message() const; virtual const char* location() const; virtual ~Exception(); }; #endif // EXCEPTION_H
Exception.cpp 源碼如下
#include "Exception.h"#include <cstring> #include <cstdlib> using namespace std; namespace DTLib { void Exception::init(const char* message, const char* file, int line) { m_message = strdup(message); if( file != NULL ) { char s1[16] = {0}; itoa(line, s1, 10); m_location = static_cast<char*>(malloc(strlen(file) + strlen(s1) + 2)); m_location = strcpy(m_location, file); m_location = strcat(m_location, ":"); m_location = strcat(m_location, s1); } else { m_location = NULL; } } Exception::Exception(const char* message) { init(message, NULL, 0); } Exception::Exception(const char* file, int line) { init(NULL, file, line); } Exception::Exception(const char* message, const char* file, int line) { init(message, file, line); } Exception::Exception(const Exception& e) { m_message = strdup(e.m_message); m_location = strdup(e.m_location); } Exception& Exception::operator= (const Exception& e) { if( this != &e ) { free(m_message); free(m_location); m_message = strdup(e.m_message); m_location = strdup(e.m_location); } return *this; } const char* Exception::message() const { return m_message; } const char* Exception::location() const { return m_location; } Exception::~Exception() { free(m_message); free(m_location); } }
main.cpp 源文件如下
#include <iostream>#include "Exception.h" using namespace std; using namespace DTLib; int main() { try { throw Exception("test", __FILE__, __LINE__); } catch(const Exception& e) { cout << "catch(const Exception& e)" << endl; cout << e.message() << endl; cout << e.location() << endl; } return 0; }
下來我們來看看編譯結(jié)果
我們看到拋出了一個異常,它的文件名是 test,行號是 11 行。我們可以在 Exception.h 頭文件添加一個宏,用來顯示文件名及行號,定義如下
#define THROW_EXCEPTION(e, m) (throw e(m, __FILE__, __LINE__))
然后將 mian.cpp 中的 throw 語句中的拋異常改為下面這樣
THROW_EXCEPTION(Exception, "test");
編譯結(jié)果如下
我們看到效果是一樣的。那么在可復(fù)用代碼庫設(shè)計時,盡量使用賣你想對象技術(shù)進行架構(gòu),盡量使用異常處理機制分類正常邏輯和異常邏輯。通過今天對異常類的學(xué)習(xí),總結(jié)如下:1、C++ 中國直接支持異常處理的概念,try ... catch ... 是 C++ 中異常處理的專用語句;2、try 語句處理的是正常代碼邏輯,catch 語句處理的是異常情況;3、同一個 try 語句可以跟上多個 catch 語句,異常處理必須嚴(yán)格匹配,不進行任何的類型轉(zhuǎn)換;4、現(xiàn)代 C++ 庫必然包含充要的異常類族,所有庫中的數(shù)據(jù)結(jié)構(gòu)都依賴于異常機制;5、異常機制能夠分離庫中代碼的正常邏輯個異常邏輯。
網(wǎng)頁名稱:異常類的構(gòu)建(四)
轉(zhuǎn)載來源:http://muchs.cn/article30/gecdpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、Google、靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)