溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

異常類的構(gòu)建(四)

發(fā)布時間:2020-07-27 13:54:11 來源:網(wǎng)絡(luò) 閱讀:846 作者:上帝之子521 欄目:軟件技術(shù)

        我們在之前學習了 C++ 中有關(guān)異常的知識,現(xiàn)在我們來重新回顧下。那么異常的格式是什么呢?便是 try ... catch ...try 語句處理正常的代碼邏輯,而 catch 語句則處理異常情況,try 語句中的異常由對應(yīng)的 catch 語句處理。格式如下

try
{
    double r = divide(1, 0);
}
catch(...)
{
    cout << "Divided by zero ..." << endl;
}

        在 C++ 中,通過 throw 語句拋出異常信息。throw 拋出的異常必須被 catch 處理,當前函數(shù)如果能處理異常,程序?qū)⒗^續(xù)往下執(zhí)行;如果當前函數(shù)無法處理異常則函數(shù)停止執(zhí)行并返回。未被處理的異常會順著函數(shù)調(diào)用棧向上傳播,直到被處理為止,否則程序?qū)⑼V箞?zhí)行。如下

異常類的構(gòu)建(四)

        同一個 try 語句可以跟上多個 catch 語句。catch 語句可以定義具體處理的異常類型,不同類型的異常由不同的 catch 語句負責處理;try 語句中可以拋出任何類型的異常,catch(...) 用于處理所有類型的異常,任何異常都只能被捕獲(catch)一次。異常處理的匹配規(guī)則如下

異常類的構(gòu)建(四)

        異常的類型可以是自定義類類型,對于類類型異常的匹配依舊是至上而下嚴格匹配,賦值兼容性原則在異常匹配中依然適用。一般而言:匹配子類異常的 catch 放在上部,匹配父類異常的 catch 放在下部。現(xiàn)代 C++ 庫中必然包含充要的異常類族,因為異常類是數(shù)據(jù)結(jié)構(gòu)類所依賴的“基礎(chǔ)設(shè)施”!舉個例子,如果我們在申請內(nèi)存失敗時便要但會一個內(nèi)存不足的異常。異常類如下圖所示

異常類的構(gòu)建(四)

        下來我們來看看異常類功能定義,如下

異常類的構(gòu)建(四)

        下來我們來創(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é)果       

異常類的構(gòu)建(四)

        我們看到拋出了一個異常,它的文件名是 test,行號是 11 行。我們可以在 Exception.h 頭文件添加一個宏,用來顯示文件名及行號,定義如下

#define THROW_EXCEPTION(e, m) (throw e(m, __FILE__, __LINE__))

        然后將 mian.cpp 中的 throw 語句中的拋異常改為下面這樣

THROW_EXCEPTION(Exception, "test");

        編譯結(jié)果如下

異常類的構(gòu)建(四)

        我們看到效果是一樣的。那么在可復用代碼庫設(shè)計時,盡量使用賣你想對象技術(shù)進行架構(gòu),盡量使用異常處理機制分類正常邏輯和異常邏輯。通過今天對異常類的學習,總結(jié)如下:1、C++ 中國直接支持異常處理的概念,try ... catch ... 是 C++ 中異常處理的專用語句;2、try 語句處理的是正常代碼邏輯,catch 語句處理的是異常情況;3、同一個 try 語句可以跟上多個 catch 語句,異常處理必須嚴格匹配,不進行任何的類型轉(zhuǎn)換;4、現(xiàn)代 C++ 庫必然包含充要的異常類族,所有庫中的數(shù)據(jù)結(jié)構(gòu)都依賴于異常機制;5、異常機制能夠分離庫中代碼的正常邏輯個異常邏輯。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI