- #1
- 2,593
- 5
Code:
void List::insert(int index, const ListItemType& newItem)
[COLOR="Red"]throw(ListIndexOutOfRangeException, ListException)[/COLOR]
{
int newLength = getLength() + 1;
if ( (index < 1) || (index > newLength) )
[COLOR="Blue"]throw ListIndexOutOfRangeException("Bad index in insert");[/COLOR]
else
{ // try to create new node and place newItem in it
ListNode *newPtr = new ListNode;
size = newLength;
newPtr->item = newItem;
// attach new node to list
if (index == 1)
{ // insert new node at beginning of list
newPtr->next = head;
head = newPtr;
}
else
{ ListNode *prev = find(index-1);
// insert new node after node
// to which prev points
newPtr->next = prev->next;
prev->next = newPtr;
} // end if
} // end insert
The above is from my notes on linked lists. Specifically it is the insert member function of linked list. I see that there are 2 possible things which may be thrown, but in the member function implementation, only one is set up to be thrown in case of out of range error. What of the other?