簡體   English   中英

異常類以處理超出范圍的錯誤-C ++

[英]Exception class to handle out-of-range errors - C++

我已經創建了一個異常類來處理模板類Vector中所有成員函數中超出范圍的錯誤,這些錯誤將索引作為參數。 我想知道如果索引超出范圍時如何拋出此類型的異常? 任何幫助將不勝感激。

這是異常類:

class IndexOutOfRangeException
{
public:
    IndexOutOfRangeException(char* str, int i, int max, int min = 0)
        :message(str),idx(i), last(max), first(min){};
    friend ostream& operator<<(ostream&, const IndexOutOfRangeException&);
private:
    char* message;
    int idx;
    int last;
    int first;
};

第一

為什么不只使用std::out_of_range

throw std::out_of_range("Tried to access index 88 of the range [0, 10]");

我想知道如果索引超出范圍時如何拋出此類型的異常?

throw IndexOutOfRangeException("info", 88, 10, 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM