returning null or throwing an exception?

Posted by MoKi on Stack Overflow See other posts from Stack Overflow or by MoKi
Published on 2014-08-20T04:15:47Z Indexed on 2014/08/20 4:20 UTC
Read the original article Hit count: 152

Filed under:
|
|

I have the following types:

typedef QPair < QTime , QTime > CalculatedTimeSlotRange;
typedef QList < CalculatedTimeSlotRange > CalculatedTimeSlotRangeList;
typedef QHash < quint8 , CalculatedTimeSlotRangeList > TimeSlotsTable;

I have a function like the following:

const CalculatedTimeSlotRangeList* TimeSlots::getCalculatedTimeSlotRangeList(const quint8 id) const
{
    QHashIterator<quint8,CalculatedTimeSlotRangeList> it(mTimeSlotsTable);
    while (it.hasNext()) {
        it.next();
        if(it.key() == id) {
            return &it.value();
        }
    }
    return NULL;
}

as you can see my function returns a null if it fails to find a key that matches id. Is this correct? or should I just throw an exception if the key does not exist? how should i throw an exception for this situation?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt