c++異常處理--創建自己的異常處理類

複習了一下c++中的異常處理!
繼承exception類
    class  myException : public std::exception
    {
    public:
        explicit myException(std::string meg) : message(meg){}
        //exception類中有一個what()虛函數,重新實現它
        virtual const char* what(){return message.c_str();}
        //不要少了這一行代碼
        virtual ~myException() throw(){}
    private:
        std::string message;    
    }

--------------------------------------------------------------------
1.function() throw(){} 
/**函數後面的throw()表示此函數不會發出異常,c++11已將其摒棄,而使用noexcept關鍵字代替 function() noexcept{}; */
2.function() throw(bad_thing){}
/**函數後面的throw(bad_thing)表示此函數會發出bad_thing類型異常*/
3.function() throw(...){}
/**函數後面的throw(...)表示此函數會發出任意類型的異常*/
4.據<<C++ primer plus>>書中表示,noexcept關鍵字也可以用作運算符,判斷操作數是否可能引發異常,如果操作數可能引發異常,則noexcept返回false,否則返回trueint function_1(int, int);
    int function_2(int, int) noexcept;

    noexcept(function_1) --> return false;
    noexcept(function_2) --> return true;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章