Here you can find the source of stackTraceToString(final Throwable throwable, final boolean expectNull)
Parameter | Description |
---|---|
throwable | The throwable for which to create the stack trace. |
expectNull | True if null should be returned when throwable is null or false to return "" when throwable is null |
public static String stackTraceToString(final Throwable throwable, final boolean expectNull)
//package com.java2s; import java.io.PrintWriter; import java.io.StringWriter; public class Main { /**/*w ww . ja v a2s.c om*/ * Returns a stack trace of the {@code Throwable} as a {@code String}. * * @param throwable * The throwable for which to create the stack trace. * @param expectNull * True if null should be returned when {@code throwable} is null or * false to return "" when {@code throwable} is null * @return null if {@code throwable} is null and {@code expectNull} is true, * "" if {@code throwable} is null and {@code expectNull} is false, * otherwise the stack trace for {@code throwable} */ public static String stackTraceToString(final Throwable throwable, final boolean expectNull) { if (throwable == null) { if (expectNull == true) { return null; } return ""; } StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); throwable.printStackTrace(printWriter); printWriter.close(); return stringWriter.toString(); } }