Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5050783

Throwable convenience method: String getStackTraceString()

    Details

    • Subcomponent:
    • Understanding:
      Cause Known
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      Name: jl125535 Date: 05/20/2004


      A DESCRIPTION OF THE REQUEST :
      Exception handling takes more lines of code than necesary

      catch (MyException e) {
          StringWriter sw = new StringWriter();
          e.printStackTrace(new PrintWriter(sw));
          // do something with sw.toString() besides sending it to a PrintWriter
          // or PrintStream with
      }



      JUSTIFICATION :
      There is very good justification for using PrintStream or PrintWriter instead of converting an exception to a string and then sending that string to the PrintStream or PrintWriter. Creating a string will require an extra object to be allocated and initialized that might not be necissary otherwise.

      However, there are many cases where I need an actual string because I am doing something different with it, like sending it to a JspWriter, storing it in a database, or sending it to some alternate mechanism. Adding a method that will provide this can simplify a lot of code that who's

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      to return a string identical to what is written to System.out, PrintWriter and PrintStream by the printStackTrace methods. Possible method names:

      String getMessageWithStrackTrace()
      String getStackTraceString()
      String getStackTraceAsString
      String getFullDescription()


      CUSTOMER SUBMITTED WORKAROUND :
      catch (MyException e) {
          StringWriter sw = new StringWriter();
          e.printStackTrace(new PrintWriter(sw));
          // do something with sw.toString() besides sending it to a PrintWriter
          // or PrintStream with
      }
      (Incident Review ID: 270111)
      ======================================================================
      The following more recent description of the same request is from 6326410 (now closed as a dupe of this CR):

      A DESCRIPTION OF THE REQUEST :
      Please consider adding a method to get a stacktrace in the form of a String from Throwable.

      JUSTIFICATION :
      We log our stacktraces over the network, and they need to be converted to Strings. We do this in a helper class currently which we used to use on all our exceptions. To be more object oriented we have overridden toString() in our exception classes so we can treat all exceptions the same without a helper class.

      However, when we want to log a full stacktrace although we have created a method toStacktrace() in our exception classes we still need to use a helper class to get the stacktrace into a string from the Java exception classes, making it so we need 2 catch blocks.

      This is picky, but it's super easy to fix, and it's bugging us.

        Issue Links

          Activity

          jleesunw Jon Lee (Inactive) created issue -
          Hide
          jjb Josh Bloch added a comment -
          BT2:EVALUATION

          This might be a reasonable convenience method.

          ###@###.### 2004-05-28
          Show
          jjb Josh Bloch added a comment - BT2:EVALUATION This might be a reasonable convenience method. ###@###.### 2004-05-28

            People

            • Assignee:
              Unassigned
              Reporter:
              jleesunw Jon Lee (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: