Java Throwable to String getStackTraceAsString(Throwable ex)

Here you can find the source of getStackTraceAsString(Throwable ex)

Description

Prints the stack trace logonservice a String so it can be put on the normal logs.

License

Apache License

Parameter

Parameter Description
ex The exception for which to write out the stack trace. If you pass null it will print the Stack trace of a newly created exception.

Return

The Stack trace logonservice a String.

Declaration

public static String getStackTraceAsString(Throwable ex) 

Method Source Code

//package com.java2s;
/*//w w w  .  j  a va2 s.c o  m
 * Copyright (c) 2015 Christian Chevalley
 * This file is part of Project Ethercis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Main {
    /**
     * Prints the stack trace logonservice a String so it can be put on the normal logs.
     * @param ex The exception for which to write out the stack trace. If you pass null it will print the Stack trace of
     * a newly created exception.
     * @return The Stack trace logonservice a String.
     */
    public static String getStackTraceAsString(Throwable ex) {
        // this is just to send the stack trace to the log file (stderr does not go there)
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream pstr = new PrintStream(baos);
        if (ex == null)
            ex = new Exception();
        ex.printStackTrace(pstr);
        return new String(baos.toByteArray());
    }
}

Related

  1. getStackTrace(Throwable throwable)
  2. getStackTrace(Throwable thrown)
  3. getStackTraceAsByteArrayOutputStream(Throwable throwable)
  4. getStackTraceAsStr(Throwable t)
  5. getStackTraceAsString(Throwable e)
  6. getStackTraceAsString(Throwable ex)
  7. getStackTraceAsString(Throwable t)
  8. getStackTraceAsString(Throwable t)
  9. getStackTraceAsString(Throwable t)