Solved

redirecting throwable.printStackTrace

Posted on 1998-03-12
7
245 Views
Last Modified: 2011-09-20
I would like to re-direct the stack trace of an exception to a String (so that I can write it to a log file).
Ideally, I would like a PrintStream class that writes to a String buffer.
0
Comment
Question by:sprinkmeier
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 100 total points
ID: 1233524
If you look at  java.lang.trowable.printStackTrace docs, you will find an impementation for:

public void printStackTrace(PrintStream s)
 
Prints this Throwable and its backtrace to the  specified print stream.  

By using this method, you can create a PrintStream derived class that writes to any place you want (disk, printer, stdout, etc) and solve your problem. The trick is to use the throwable implementation.
0
 
LVL 2

Author Comment

by:sprinkmeier
ID: 1233525
OK, I know hot to print the stack trace to a PrintStream, but how do I create a Printstream that outputs to a String buffer?

I know it's in the DOC's somewhere, and if I had them, I would not be asking this question.
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233526
OK,

ByteArrayOutputStream out = new ByteArrayOutputStream();

PrintStream ps = new PrintStream(out);

// You now have a Print Stream that writes to a buffer array.

...

// Inside an exception handler you could now use:

printStackTrace(ps);

String Buffer = ps.toString();

// Now you can use Buffer in whatever way you require.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 2

Author Comment

by:sprinkmeier
ID: 1233527
/*

To Compile:
  javac exc_2_string.java
 
To Run:
  java  exc_2_string

*/
class exc_2_string
{

  public static void main(String args[])
  {
    try{
      System.out.println("Throwing a wobbly");
      // create and throw an exception
      Exception x = new Exception("ASDF");
      throw(x);
    } catch (Exception x) {
      System.out.println("Exception caught");
      // Create a place to stick the data
      java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
/*            
      java.io.PrintStream ps = new java.io.PrintStream(out);

  PrintStream is replaced, use PrintWriter instead. Only differance
  seems to be that the PrintWriter must be flush()-ed before you
  examine the ByteArrayOutputStream (alternativley, create the
  PrintWriter with the boolean autoFlush set to reue, ie.
      java.io.PrintWriter pw = new java.io.PrintWriter(out, true);
*/
      // create a stream to accept the data
      java.io.PrintWriter pw = new java.io.PrintWriter(out);
      // write to the stream
      x.printStackTrace(pw);
      // make sure the stream is flushed
      pw.flush();
      // copy the data from the storage object
      String buff = out.toString();
      // cleanup
      pw.close();
      pw = null;
      out.reset();
      out = null;
      // print it
      System.out.println("Via String: " + buff);

      System.out.println("Direct to System.out:");
      x.printStackTrace(System.out);

    }
  }
}
 
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233528
Hum? What do you want me to do with this code ? :-)
0
 
LVL 2

Author Comment

by:sprinkmeier
ID: 1233529
Just thought I'd give value for money to anyone who buys this question later.

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1233530
OH, Good Idea.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question