title: How do I append to a Java exception?
body: <p>I am new to Java and exceptions in general.</p>
  
  <p>In my prior C/Perl programming days, when I wrote a library function, errors were passed back by a boolean flag, plus some kind of string with a human-friendly (or programmer-friendly) error message.  Java and C++ have exceptions, which is handy because they include stack traces.</p>
  
  <p>I often find when I catch an exception, I want to add my two cents, then pass it along.</p>
  
  <p>How can this be done?  I don't want to throw away the whole stack trace... I don't know how deep the failure occurred and for what reason.</p>
  
  <p>I have a little utility library to convert a stack track (from an Exception object) into a string.  I guess I could append this to my new exception message, but it seems like a hack.</p>
  
  <p>Below is an example method. Advices?</p>
  
  <pre><code>
      public void foo(String[] input_array) {
          for (int i = 0; i &lt; input_array.length; ++i) {
              String input = input_array[i];
              try {
                  bar(input);
              }
              catch (Exception e) {
                  throw new Exception("Failed to process input [" 
                          + ((null == input) ? "null" : input)
                          + "] at index " + i + ": " + Arrays.toString(input_array) 
                          + "\n" + e);
              }
          }
      }
  </code></pre>