title: Throwing a Linked List of Exceptions in Java
body: <p>I have a function that loops while doing something that could throw an exception.  Looks something like this:</p>
  
  <pre><code>public void myFunction() throws MyException {
      while(stuff) {
          try {
              DoSomething() // throws an exception
          }
          catch (Exception ex) {
              throw new MyException(some, stuff, of, mine, ex);
          }
      }
  }
  </code></pre>
  
  <p>The error causing the exception is recoverable.  It can be something like an SQL error in a single update statement where the while loop executes a series of update statements.  Or a parsing error in a single piece of data, where the loop is processing multiple pieces of data.  I need to pass the exception further up the chain so the GUI part of the program can process it, handle it and pass on the error to the user.  But I don't want to kill the loop in this particular function.  The other things it's doing might not be invalid.  The error that caused the exception might not be fatal to the function.</p>
  
  <p>So my question is this:  <strong>Is it an acceptable practice to build linked lists of custom exceptions (where each exception is a node, and the exception thrown is the head of the list) and then throw the head of the list (if any) once the loop finishes?</strong></p>
  
  <p>Has anyone ever seen this done? Can anyone think of any potential problems with doing this?  Can anyone think of other, better ways to handle the root problem: the need to pass up multiple unrelated exceptions with out exiting the function until it is done? </p>
  
  <p>Here's an example of how the linking and throw might be implemented very simply:</p>
  
  <pre><code>public void myFunction() throws MyException {
      MyException head = null;
      while(stuff) {
          try {
              DoSomething() // throws an exception
          }
          catch (Exception ex) {
              MyException tmp = new MyException(some, stuff, of, mine, ex);
              tmp.next(head);
              head = tmp;
          }
      }
      if(head != null)
         throw head;
  }
  </code></pre>