Solved

redirecting throwable.printStackTrace

Posted on 1998-03-12
7
203 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now