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.
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.
- duplicates
-
JDK-6326410 Throwable could use a method to get a stacktrace as a String
- Closed