title: Trying to catch a failed allocation inside of a recursive function: Unhandled exception/Stack overflow
body: <p>This is a problem I'm trying to solve for a school assignment. I've created a single linked list class that contains a Node(ie. a struct that holds a character "item", and a Node pointer "next") and a Node pointer head. As specified by the assignment, I'm required to implement several operations on the linked list recursively. I've managed to get all of this working fine.</p>
  
  <p>However, as the next step of the assignment I'm asked to: <em>"write a test program to determine what the longest possible linked list is. (You will be testing the heap size). You can do this easily since the append function is defined to return true if successful and false otherwise."</em></p>
  
  <p>In class, it was mentioned that we should use something along the lines of </p>
  
  <pre><code>try{ //Allocate a new node
       // Throw an exception if it fails
  }
  catch(...){
       // Do something with the caught exception
  }
  </code></pre>
  
  <p>And for the life of me, I can't get this to work! I'm getting the following error:</p>
  
  <p><code>Unhandled exception at 0x7765DED4 (ntdll.dll) in Assignment.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00352FFC).</code></p>
  
  <p>This is my first time dealing with exceptions, so maybe I'm missing something, but I've been trawling google and my collection of textbooks for hours and I'm officially stuck.</p>
  
  <p>Here is the code in question:</p>
  
  <pre><code>// RECURSIVELY appends an element to the end of the list
  bool LinkedList::append(char newChar){ 
      if (head == nullptr){ // Adds a node to empty list
          head = new Node{ newChar, nullptr };
          return true;
      }
      return appendHelper(newChar, head); // Call the recursive helper function
  }
  
  // Helper function for append
  bool LinkedList::appendHelper(char newChar, Node* currentNode){     
      if (currentNode-&gt;next == nullptr){ // Adds a node to the end of a list
  
          // HERE'S WHERE THE TROUBLE BEGINS:
          try{
              currentNode-&gt;next = new Node{ newChar, nullptr }; // From what I've read, this should throw std::bad_alloc if it fails?
          }
          catch(...){ // The "..." should catch ANY exception, right?
              return false; // Return false, so that I'll know allocation failed
          }
          return true; // Mustn't have encountered an exception; return true.
      }
      return appendHelper(newChar, currentNode-&gt;next);
  }
  </code></pre>
  
  <p>I'm fairly confident that my code, aside from the exception handling, works. Ie. Adding a node using:</p>
  
  <pre><code>currentNode-&gt;next = new Node{ newChar, nullptr };
  </code></pre>
  
  <p>seems to work fine. The discussion we had in class left me with the impression that I should be able to catch the exception when the allocation fails, return a false, then continue with the rest of my program/experiement...</p>
  
  <p>Any idea where am I going wrong, or how can I solve this? I'm starting to think that I should just iterate a counter and print it to the screen every time I call the <code>append()</code> function, then just record this number when it crashes... But this doesn't seem very elegant, nor do I feel that this is what was expected.</p>