I am trying to catch exceptions on the double linkedlist. But I do not know how execute it so it gives errors like "not enough memory, deleting an element from an empty list, and any other exceptions that can be found". When I run it just outputs the double linked list without the exceptions. Thanks!
#include <cstdlib>
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
Node* prev;
};
void printFront(Node*head)
{
Node* firstToLast = head;
while (firstToLast != nullptr)
{
cout << firstToLast->data << endl;
firstToLast = firstToLast->next;
}
}
void printBack(Node* tail)
{
Node* firstToLast = tail;
while (firstToLast != nullptr)
{
cout << firstToLast->data << endl;
firstToLast = firstToLast->prev;
}
}
void main()
{
Node* head;
Node* tail;
//1st node
Node* node = new Node();
node->data = 4;
node->next = nullptr;
node->prev = nullptr;
head = node;
tail = node;
//2nd node
node = new Node();
node->data = 5;
node->next = nullptr;
node->prev = tail;
tail->next = node;
tail = node;
//3rd node
node = new Node();
node->data = 6;
node->next = nullptr;
node->prev = tail;
tail->next = node;
tail = node;
printFront(head);
try
{
if (tail != head)
{
throw 2;
}
}
catch (exception x)
{
cout << "The error is ... " << x.what();
}
}