Home > other >  Doing exceptions on double linked lists but does not throw out the error?
Doing exceptions on double linked lists but does not throw out the error?

Time:07-25

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

CodePudding user response:

Your try/catch makes little sense.

If you want to catch deleting an element from an empty list, you should

  1. Write a function that deletes an element from a list.
  2. Make it throw an exception when the input list is empty.
  3. Finally, catch that exception when you try calling said function. Not inside the function itself. That's not what exceptions are for.

You can throw exceptions of almost any data type, but that's bad practice. Create an exception class and throw objects of that class.

 class MyListException
 {
   // add an explicit constructor with a string argument, 
   // and a virtual destructor
   public:
     std::string what() { return m_what; }
   private:
     std::string m_what;
 };

 // somewhere
 if (...) {
    throw MyListException("deleting from an empty list");
 }

 // somewhere else
 try {
    // something 
 }
 catch (const MyListException& e) {
    // std::cout << ... << ... << e.what(); 
 }

If you want to catch a "not enough memory" condition, you need to look up which standard exception is thrown when new fails to allocate memory. Write a function that allocates an awful lot of nodes, and try calling that function, catching that exception. Note that on some modern operating systems you are not likely to get such an exception (look up "OOM killer" if you want to know more).

CodePudding user response:

You could catch any type (as an integer) but is considered a bad practice (it's funny there's an example throwing 42 in cpp reference) and you can have unexpected results.

However, the "right" way is throwing a std::runtime_error and then catching it.

You can simply add error text as

std::runtime_error("my error");

Or you could do something more elaborate by creating a class that inherits from std::runtime_error and adding more information to it.

Finally in your catch, you can get the error description as:

catch (const std::exception& e)
{
    std::cout << "Failed  with: " << e.what();
}

This is a great lecture about exceptions that may help you a lot :)

  •  Tags:  
  • c
  • Related