Solved

redirecting throwable.printStackTrace

Posted on 1998-03-12
7
227 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 74
add projects t working set in maven 2 41
Setup GlassFish 4 35
How to log java errors in tomcat 8 24
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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.

756 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