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
Solved

redirecting throwable.printStackTrace

Posted on 1998-03-12
7
221 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
varialbe initialization 11 37
difference between sorce folder and folder in eclipise 3 28
Convert from a json string array to a Java object 3 51
Chrome and Firefox Java 5 49
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

789 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