Solved

redirecting throwable.printStackTrace

Posted on 1998-03-12
7
210 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
Securing Jmx Console and web console 2 65
VB Script to add site to Java Exception List 4 53
object oriented programming comparison 5 55
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

896 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