Created method to get an exception stack trace as a string

This commit is contained in:
Tim Visée 2015-11-22 21:01:31 +01:00
parent 215fedc585
commit da662afcf4

View File

@ -4,6 +4,9 @@ import net.ricecode.similarity.LevenshteinDistanceStrategy;
import net.ricecode.similarity.StringSimilarityService; import net.ricecode.similarity.StringSimilarityService;
import net.ricecode.similarity.StringSimilarityServiceImpl; import net.ricecode.similarity.StringSimilarityServiceImpl;
import java.io.PrintWriter;
import java.io.StringWriter;
/** /**
* Utility class for String operations. * Utility class for String operations.
*/ */
@ -84,4 +87,22 @@ public class StringUtils {
return sb.toString(); return sb.toString();
} }
/**
* Get a full stack trace of an exception as a string.
*
* @param exception The exception.
*
* @return Stack trace as a string.
*/
public static String getStackTrace(Exception exception) {
// Create a string and print writer to print the stack trace into
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
// Print the stack trace into the print writer
exception.printStackTrace(printWriter);
// Return the result as a string
return stringWriter.toString();
}
} }