JAVA How to JAVA Development tutorials and how to

3Mar/100

How to save JAVA Exception printStackTrace on String

Probably you've used sometines the Exception object in try/catch block to print the exception stack trace that help us to find where the line of code where the exception is thrown, but more than once i wanted to get that stack trace in some String variable for example, maybe to save in some auditory table or something like that. I read a little bit about the method getStackTrace but really it doesn't give me the full stack trace.

Well, to do that, first you have to understand that when you call ex.printStackTrace() you print a characters stream on screen, so the way to get that characters stream is using the overloaded version of printStackTrace(PrintWriter objWriter) that receives a PrintWriter object to transfer that characters stream to the PrintWriter object, then you can use that writer to "write" it's stream on something like a file or a simple String.

Check this sample code:

import java.io.File;
import java.io.IOException;
class MyClass {
	public static void main(String[] arguments)
	{
		try
		{
			(new MyClass()).readInexistentFile();
		}
		catch(IOException ex)
		{
			//Catch the stack trace?
		}
	}

	public void readInexistentFile() throws IOException
	{
		File myFile = new File("hi6.txt");
			FileReader myReader = new FileReader(myFile);
			myReader.read();
	}
}

...

As you can see, i want to read a file that probably doesn't exists so it will throw a IOException with some stack trace like this (ignore the number of code lines):

java.io.FileNotFoundException: hi6.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileReader.<init>(FileReader.java:55)
        at MyClass.readInexistentFile(MyClass.java:94)
        at MyClass.main(MyClass.java:82)

I want THAT STACK TRACE IN A STRING!! . Let's modify the code in the catch block to look like this (only catch block below):

catch(IOException ex)
		{
                    StringWriter writerStr = new StringWriter();
                    PrintWriter myPrinter = new PrintWriter(writerStr);
                    ex.printStackTrace(myPrinter);
                    String stackTraceStr = writerStr.toString();
                    //writerStr.getBuffer();
                    System.out.println("This is the stack: " + stackTraceStr);
		}

As you can see, overloaded version of printStackTrace() transfers the characters stream to PrintWriter object, that can write the stream to a File, a String or something like that according to constructor that we've used to istantiate it. In our case we used StringWriter but you can use FileWriter to write to some file for example. After that, we can access to stack trace string with writerStr.toString() or if we want a StringBuffer, with writerStr.getBuffer().

You can download the entire source code >>HERE<< and i want to say that i learned that from Real's How to.

import java.io.File;
import java.io.IOException;
class MyClass {
public static void main(String[] arguments)
{
try
{
(new MyClass()).readInexistentFile();
}
catch(IOException ex)
{
//Catch the stack trace?
}
}

public void readInexistentFile() throws IOException
{
File myFile = new File("hi6.txt");
FileReader myReader = new FileReader(myFile);
myReader.read();
}
}

Share and Enjoy

  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Technorati
  • MySpace
  • Sphinn
  • Print
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Identi.ca
  • LinkedIn
  • Meneame
  • Netvibes
  • Yigg
  • Reddit
  • RSS
  • Tumblr
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.