简体   繁体   中英

Exception class to handle out-of-range errors - C++

I've created an exception class to handle out-of-range errors in all my member functions in my template class Vector that take an index as a parameter. I want to know how do I throw an exception of this type if the index is out-of-range? Any help would be greatly appreciated.

Here is the exception class:

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;
};

First

Why not just use std::out_of_range ?

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

I want to know how do I throw an exception of this type if the index is out-of-range?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM