Here you can find the source of stackTrace(Throwable exception)
Throwable
as a String
.
Parameter | Description |
---|---|
e | - an instance of <code>Throwable</code> |
String
public static String stackTrace(Throwable exception)
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; public class Main { /**// w w w .j a v a 2 s . co m * Returns the stack trace of an <code>Throwable</code> * as a <code>String</code>. * * @param e - an instance of <code>Throwable</code> * * @return the exception's stack trace as a * <code>String</code> */ public static String stackTrace(Throwable exception) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(outputStream); exception.printStackTrace(printStream); return outputStream.toString(); } public static String toString(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); copy(inputStream, outputStream); return outputStream.toString(); } /** * Reads the bytes from the input and writes them to the * output. * * @param input - the input stream * @param output - the output stream * */ public static void copy(InputStream in, OutputStream out) throws IOException { byte[] b = new byte[10240]; // 10K buffer int result = in.read(b); while (result != -1) { out.write(b, 0, result); result = in.read(b); } } }