溫馨提示×

溫馨提示×

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

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

C++ 異常拋出以及捕獲

發(fā)布時間:2020-07-02 00:14:47 來源:網(wǎng)絡(luò) 閱讀:743 作者:CLEVERlBOY 欄目:編程語言

臨近離職,決定補一下之前一直沒來得及學(xué)的C++11的知識,突然翻到了異常處理,感覺有點好玩就自己寫了個測試程序,然后三觀徹底被顛覆了。
源代碼如下:

#include <iostream>
#include <string>
#include <exception>

void speak(int i)
{
    if(i <= 0)
    {
        throw "System get a wrong...";
    }
}

void main()
{
    try
    {
        speak(-1);
    }
    catch(std::exception e)
    {
        std::cerr << "exception info:" << e.what() << std::endl;
    }
}

很簡單對不對,編譯也沒錯,然后運行就掛了,通過Debug跟蹤,發(fā)現(xiàn)是這么一行掛了
catch(std::exception e),然后各種懷疑,各種改,就差重裝系統(tǒng)了。

無意中改了這么一句,然后自己把異常處理了,改后的代碼如下:

#include <iostream>
#include <string>
#include <exception>

void speak(int i)
{
    if(i <= 0)
    {
        throw std::exception("System get a wrong...");
    }
}

void main()
{
    try
    {
        speak(-1);
    }
    catch(std::exception e)
    {
        std::cerr << "exception info:" << e.what() << std::endl;
    }
}

注意throw 那一塊,很懵,可能是exception 是一個explict的類吧,沒太大的功夫研究,記住就行了。程序之所以掛掉,可能是因為這里沒能捕獲,直接交給操作系統(tǒng)了。

==================補充說明===================
上午說的那個exception類的聲明,我貼在下面,我在不同的平臺下又遇到問題了:

_STDEXT_BEGIN
class exception
    {   // base of all library exceptions
public:

    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew)
        {   // register a handler for _Raise calls
        const _STD _Prhand _Pold = _STD _Raise_handler;
        _STD _Raise_handler = _Pnew;
        return (_Pold);
        }

    ====這里就是為啥char *無法自動轉(zhuǎn)換為exception的原因了
    // this constructor is necessary to compile 
    // successfully header new for _HAS_EXCEPTIONS==0 scenario
    explicit __CLR_OR_THIS_CALL exception(const char *_Message = _MESG("unknown"), int x=1)
        _THROW0()
        : _Ptr(_Message)
        {   // construct from message string
                (void)x;
        }

    __CLR_OR_THIS_CALL exception(const exception& _Right) _THROW0()
        : _Ptr(_Right._Ptr)
        {   // construct by copying _Right
        }

    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) _THROW0()
        {   // assign _Right
        _Ptr = _Right._Ptr;
        return (*this);
        }

    virtual __CLR_OR_THIS_CALL ~exception()
        {   // destroy the object
        }

    virtual const char * __CLR_OR_THIS_CALL what() const _THROW0()
        {   // return pointer to message string
        return (_Ptr != 0 ? _Ptr : "unknown exception");
        }

    void __CLR_OR_THIS_CALL _Raise() const
        {   // raise the exception
        if (_STD _Raise_handler != 0)
            (*_STD _Raise_handler)(*this);  // call raise handler if present

        _Doraise(); // call the protected virtual
        _RAISE(*this);  // raise this exception
        }

protected:
    virtual void __CLR_OR_THIS_CALL _Doraise() const
        {   // perform class-specific exception handling
        }

protected:
    const char *_Ptr;   // the message pointer
    };
_STDEXT_END

我遇到的另一個問題是在gunc上,exception并沒有帶char 的構(gòu)造函數(shù),要想通過char 構(gòu)造exception,就只能使用有這個構(gòu)造函數(shù)的子類;但是在windows平臺上,exception這個類卻又有這個構(gòu)造函數(shù)。這里記得就行了。

向AI問一下細節(jié)

免責(zé)聲明:本站發(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