A more involved example is provided by revising our old list-based stack program to use exception handling. itemtype.h listnode.h header file that sets up ListNodeClass listnode.cpp list.h header file that sets up ListClass list.cpp modified to use exceptions stack.h header file that sets up the abstract base class for stacks lststack.h header file that sets up the derived class for list-based stacks lststack.cpp modified to use exceptions reverse.cpp Let's look first at the list.cpp file. In particular, let's look at the GetNode function. Part of the code for this function is repeated below:NodePtr = new ListNodeClass(Item, NextPtr);if (NodePtr == NULL) { throw "Cannot allocate memory"; }Note that the value of the exception is a literal string. It is stored as a character array. Remember that an array name is seen as a pointer to the first character in the array. Thus, the type of the exception is seen as char *.Warning: Check your compiler to see how the new operation behaves when it cannot allocate space. According to the most recent C++ standard as of this writing, it should throw an exception of type bad_alloc, but Visual C++ 5.0, the compiler used to check our examples, instead just returns a value of NULL, which is what many older compilers do. This compiler supposedly has a way to tell new to use exceptions instead, but even then it does not match the current standard since the type of the exception is xalloc.In the same file, look at the ListClass constructor. It allocates a dummy node for the front of the list. If an exception of type char * is raised when trying to do this, an error message is printed that contains the (string) value of the exception and the program is exited. Of course, the string printed is the literal string used when the exception is thrown by GetNode: "Cannot allocate memory". The choice to exit the program was made because it seemed to be a rather serious error if we cannot even allocate space for the dummy node of a list.The InsertFront function uses GetNode, as shown below, to create a new node holding the data to be inserted. (The InsertRear and InsertInOrder functions also do this.) If an exception of type char * is caught, then we print an error message containing the (string) value of the exception and re-throw the exception. Re-throwing the exception is accomplished by the throw statement below. Whatever called InsertFront should try to catch this exception and take appropriate action. (If not, the program will terminate.)try { NodePtr = GetNode(Item, Front->Next); }catch(char * Str) { cout << "Error: Could not insert at front -- " << Str << endl; throw; }The RemoveFront function checks the count of how many items are on the list before trying to remove. If that number is zero, it throws an exception. Note that this one is an integer exception. The relevant section of code follows:if (Count == 0) { throw Count; }Next, look at the lststack.cpp file. It uses InsertFront to implement the Push operation, and so needs to check if InsertFront has thrown an exception. If a char * exception is raised, Push prints an error message. However, we have chosen not to exit the program, thinking that perhaps this error is not fatal and that the user might want to continue in spite of the error.In the same file you see the Pop function. It checks for an integer exception, since that is the type that might be raised by the RemoveFront function that is called by Pop. If such an exception is caught, a warning message is printed.We could also have chosen to re-throw the exception if either Push or Pop catches an exception. Then the reverse.cpp application program would need to contain code to catch such exceptions, much like we did in the above array-based stack program. It seemed to be sufficient to let it go as is.